Article

Object Oriented Concepts in Java – Part 2

Page: 1 2 3 4 5 6 7 8 9 10 Next

Advanced Inheritance: Overriding Methods

I covered inheritance in Part One of this article, but I left out one advanced issue for the sake of brevity that I'd like to cover now: overriding methods. As you know, an object of class CoconutTree inherits all of the features of the Tree class, on which it is based. Thus, CoconutTrees have grow methods just like Trees do.

But what if you wanted CoconutTrees to sprout new coconuts when they grew? Sure, you could call the growNut method every time you caused a CoconutTree to grow, but it would be nicer if you could treat Trees and CoconutTrees exactly the same way (i.e. call their grow method) and have them both do what they're supposed to do when objects of their type grow.

To have the same method do something different in a subclass, you must override that method with a new definition in the subclass. Put simply, you can re-declare the grow method in the CoconutTree class to make it do something different! Here's a new definition for grow that you can add to your CoconutTree class:

public void grow() {
height = height + 1;
growNut();
}

Simple, right? But what if you added new functionality to the grow method in the Tree class? How could you make sure that this was inherited by the CoconutTree class? Like in our discussion of overloaded methods, where we implemented a simple method by calling a special case of the more complicated method, we can implement a new definition for a method in a subclass by referring to its definition in the superclass:

 public void grow() {        
   super.grow();        
   growNut();        
 }

The super.grow() line invokes the version of grow defined in the superclass, thus saving us from having to reinvent the wheel. This is especially handy when you are creating a class that extends a class for which you do not have the source code (e.g. a class file provided by another developer). By simply calling the superclass versions of the methods you are overriding, you can ensure that your objects aren't losing any functionality.

Constructors may be overridden just like normal methods. Here's a set of constructors for the CoconutTree class, along with the new declaration of the numNuts property without an initial value:

 private int numNuts;        
       
 public CoconutTree() {        
   super();        
   numNuts = 0;        
 }        
       
 public CoconutTree(int height) {        
   super(height);        
   numNuts = 0;        
 }        
       
 public CoconutTree(int height, int numNuts) {        
   super(height);        
   if (numNuts < 0) this.numNuts = 0;        
   else this.numNuts = numNuts;        
 }

The first two constructors override their equivalents in the Tree class, while the third is completely new. Notice that we call the constructor of the superclass as super(). All three of our constructors call a constructor in the superclass to ensure that we are not losing any functionality.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links