Abstract Methods Abstract Base Classes


Overriding Base Class Methods

So far you have only seen examples of inheritance where the derived class fully accepted the behavior provided by its base class. This section will show you how to override base class behavior in the derived class by overriding base class methods.

To override a base class method in a derived class you will need to re-define the method with the exact signature in the derived class. The overriding derived class method must also return the same type as the overridden base class method. (The Java compiler will complain if you try otherwise!) Let’s take a look at a simple example. Figure 11-8 gives a UML class diagram for the BaseClass and DerivedClass classes.

image from book
Figure 11-8: UML Class Diagram For BaseClass & DerivedClass

Referring to figure 11-8 — notice that DerivedClass now has a public method named printMessage(). BaseClass remains unchanged. Example 11.8 gives the source code for the modified version of DerivedClass.

Example 11.8: DerivedClass.java (mod 1)

image from book
 1     public class DerivedClass extends BaseClass { 2       private String _message = null; 3 4        public DerivedClass(String message){ 5          super(message); 6          _message = message; 7        } 8 9        public DerivedClass(){ 10         this("DerivedClass message!"); 11       } 12 13      public void printMessage(){ 14        System.out.println("Message from DerivedClass: " + _message); 15      } 16    }
image from book

The only change to DerivedClass is the addition of the printMessage() method starting on line 13. DerivedClass’s version of printMessage() overrides the BaseClass version. How does this affect the behavior of these two classes? A good way to explore this issue is to recompile and run the DriverApplication given in example 11.3. Figure 11-9 shows the results of running the program using a modified version of DerivedClass.

image from book
Figure 11-9: Results of Running Example 11.3 with Modified Version of DerivedClass

Referring to figure 11-9 — compare these results with those of figure 11-4. The first message is the same, which is as it should be. The bcr1 reference points to a BaseClass object. The second message is different though. Why is this so? The bcr2 reference is pointing to a DerivedClass object. When the printMessage() method is called on the DerivedClass object via the BaseClass reference the overriding printMessage() method provided in DerivedClass is called. This is an example of polymorphic behavior. A base class reference, bcr2, points to a derived class object. Via the base class reference you call a method provided by the base class interface but is overridden in the derived class and, voila, you have polymorphic behavior. Pretty cool, huh?

Quick Review

Derived classes can override base class behavior by providing overriding methods. An overriding method is a method in a derived class that has the same method signature as the base class method it is intending to override. Overriding methods can be called polymorphically via a base class reference that points to a derived class object.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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