Overriding


You have already seen method overloading, where a method name is reused in a class definition. Java also supports method overriding, which looks like it ought to be similar to overloading because the spellings are so similar. In fact, overriding is indeed a kind of method name reuse. In this case, the reuse is within an inheritance hierarchy.

To continue this ongoing example, the Officer class is the bottom member of a three-level hierarchy. This is shown in Figure 8.5.


Figure 8.5: Inheritance of Officer

In the original version of Employee, you imagined a printCheck() method. This method is inherited by Worker, Manager, Officer, and any other subclasses of Employee, immediate or indirect, that you might create in the future.

But what happens if a class inherits a method that is not appropriate to that class's operation? For example, printCheck() might print checks on a monochrome printer that is loaded with low-cost blank check forms. That's fine for ordinary workers, and even for managers, but officers need to have their checks printed by a special color printer, on high-grade fancy paper. So the inherited version of printCheck() just won't do.

The solution is to override Officer's inherited version of printCheck(). You do this simply by putting into the Officer code a version of printCheck() that does what you want it to do. The code looks like this:

 1. public class Officer extends Manager  2. {  3.   int nYrsOnBoard;  4.  5.   Officer(int nWorkers, int initialNYrs)  6.   {  7.     super(850000f, nWorkers);  8.     nYrsOnBoard = initialNYrs;  9.   } 10. 11.   void printCheck() 12.   { 13.     // Whatever, and make it fancy. 14.   } 15. 16.   public static void main(String[] args) 17.   { 18.     Officer julius = new Officer(25, 50); 19.     julius.printCheck();     // Fancy 20.     Worker dagwood = new Worker(44444.44f); 21.     dagwood.printCheck();    // Plain 22.   } 23. }

Again, we've left out the details of how to print a check. The import point is line 11, where you have a declaration of printCheck() that looks just like the version in Employee. The declarations are the same, but the bodies are different. Look at the main() code. On line 19, you call printCheck() on an officer. The overriding (fancy) version of the method will be called. On line 21, you call printCheck() on a worker. Since Worker does not override the method, the inherited (plain) version is called.

In order for one method to override another, the superclass version and the subclass version must have identical return types, method names, and argument list types. It is illegal for the return types to be different if the method names and argument list types match. If the names or argument lists are different, that's legal but it isn't overriding.

Overriding allows you to use a very powerful kind of polymorphism, which will be explained in the next section.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net