Basic HTML version of Foils prepared Sept 6 1998

Foil 31 Comments on Overloading and Overriding in Classes

From Java Tutorial 98-2: Java Language and Object Oriented Techniques CPS606 Fall Semester 1999 -- Sept 7 1999. by Geoffrey C. Fox, Nancy McCracken


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!)



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Mon Sep 6 1999