Comments on Overloading and Overriding in Classes
Overriding Methods (where child class provides method with same signature as method in parent)
- To override a method, a subclass of the class that originally declared the method must declare a method with the same name, return type (or a subclass of that return type), and same parameter list.
- When the method is invoked on an instance of the subclass, the new method is called rather than the original method.
- The overridden method can be invoked using the super variable.
- Super can be used to refer to instance variables in the superclass as well.
Overloading (where a class can provide a set of methods all with the same name, but with different signatures): The signature is defined (as in Arnold-Gosling book) by
- Lowest conversion cost of parameter list, based on type and number of parameters. Return type and declaration order not important.
- Java will declare an error if method is invoked where there is not one with a unique signature which matches call after removing less specific methods (use of objects in hierarchy can cause lots of confusing matches!)