Tutorials : The Pitfalls of Inheritance :

Restricting inheritance…

Sometimes in your design you would like to stop some classes and methods being inherited. The final keyword in Java achieves this functionality. When you declare a class to be final, no other sub class of it can be created. Similarly, when you declare a method to be final, no sub class can override that method.

Overriding member variables…

So far we have talked about how can you override the methods declared in the super class. It is also possible to hide the member variables declared in the super class, by declaring a member variable in the sub class with the same name as in super class. The type of the field does not matter, only the name needs to match. Notice that when you hide a member variable in the sub class, you can still access the super class variable by using the super keyword. This means that the member variables cannot be overridden but can only be hidden. It is important to notice this subtle distinction between overriding and hiding.

The logic behind that Java allows the hiding of member variables, is that you can then create a sub class of any super class without detailed knowledge of its internal implementation. This is more in the line of encapsulation.

How it works…

We have just seen how powerful inheritance mechanism is and how relatively it is to implement in real world. We will later in this article examine some of the pitfalls with this inheritance mechanism but for the time being, let us see how the inheritance works in Java.

Object Initialization and Inheritance

When an object is initialized, all the instance variables defined within the class of that object and also all the instance variables defined in its super class get initialized. Consider the following example in Listing 3.
public class Vehicle

{

   private String registration;

    

   public void accelarate()

   {

 

   }

   public void stop()

   {

       //stop the engine

 

   }

}

 

 

public class Car extends Vehicle

{

    private String model;

 

    public Car()

    {

 

    }

 

    public void stop()

    {

        //stop the engine

        //lock the steering wheel

    }

 

    public void switchOnWiper()

    {

 

    }

 

 

}
Listing 3 A Simple inheritance model

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.