Computer Science : Class Relationships

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

Example Question #8 : Class Design

What is the difference between extending and implementing?

Possible Answers:

You can extend an interface and implement a subclass. Subclasses can be implemented as frequently as needed but you can only extend a single interface.

Extend refers to a relation between a superclass and subclass. Implementing refers to an interface. You can extend only a single class but you can implement multiple interfaces.

Extend refers to a relation between a superclass and subclass. Implementing refers to an interface. You can implement only a single interface but you can implement multiple classes.

Correct answer:

Extend refers to a relation between a superclass and subclass. Implementing refers to an interface. You can extend only a single class but you can implement multiple interfaces.

Explanation:

By default, all classes are extensions of the Object class and therefore are extending a single class. Due to that concept, implementation was made with the idea that notion of only being able to extend a single class. Thus they allow you to implement an infinite amount of classes.

Example Question #9 : Class Design

Describe the difference between overloading a method and overriding a method.

Possible Answers:

Overloading is the same method name and different parameters. Overwriting is rewriting the body of the definition.

Overloading is the same method name, same parameters but the name of the parameter is different. Overwriting is calling the same method in the same class.

Overwriting is the same method name and different parameters. Overloading is rewriting the body of the definition.

Overloaded methods and overridden methods both take in the same methods but overridden methods return a different parameter.

Correct answer:

Overloading is the same method name and different parameters. Overwriting is rewriting the body of the definition.

Explanation:

When you override a method, you are completely replacing the functionality of the method by overshadowing the method in the parent child with the one in the child class.

 

Overloading a method just allows you to have the same method name to have multiple meanings or uses. An example is '+' operator. In languages like Java, you can use '+' to append strings, add doubles, or add integers.

Learning Tools by Varsity Tutors