Overloading vs. Overriding
- Do you have to pay attention to the return type when overloading a method?
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?
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?
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?
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?
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?
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?
You can not make a new object without invoking a constructor.
- How are objects initialized?
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?
Every class, including abstract class, must have a constructor.
- What are two key points to remember about constructors?
- Constructors do not have a return type
- Their names must exactly match the class name.