Overloading vs. Overriding
- Do you have to pay attention to the return type when overloading a method?
Answer:
No. When you overload a method (by changing the parameters), the return type does not have to match that of the superclass version.
- What is the difference between arguments and parameters?
Answer:
Parameters are the elements in the methods signature that indicate what the method must receive when its inovked.
- Which characteristics must an overriding method in a subclass contain?
Answer:
When a subclass wants to override an inherited method the subclass must define a method that matches the inherited version exactly (For reference types covariant returns are allowed).
- What is a covariant return type?
Answer:
As of Java 5, you are allowed to change the return type in the overriding method as long as the new return type is a subtype of the declared return type of the overridden method.
- What role does the return type play with regards to overloading and overriding?
Answer:
Know the following 2 facts:
- Overloaded methods can change the return type
- overriding methods can change the return type if the overridden method is a subclass of the Superclass.
Covariant Return Type
- For primitive return types, what must be true of the overriding method?
Answer:
When overriding a method, the method in the subclass must match the inherited version exactly.
- What task must be completed to create a new object?
Answer:
You can not make a new object without invoking a constructor.
- How are objects initialized?
Answer:
Constructors are the code that runs whenever you use the keyword new.
Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
- What must every class possess?
Answer:
Every class, including abstract class, must have a constructor.
- What are two key points to remember about constructors?
Answer:
- Constructors do not have a return type
- Their names must exactly match the class name.