Tutorials : The Pitfalls of Inheritance :

Following this example, we would do the following to create an instance of the Car object:

//create a new instance of the Car object

Car car = new Car();

In this, the instance variable of the Car class named model and also the instance variable registration of the super class Vehicle both will get initialized to their default values. By default, the Java Virtual Machine will allocate enough space for all the instance variables of the Object’s own class and all the instance variable in all its super classes. Do not forget that in Java you can have multi-level inheritance, that is a Car IS A Automobile IS A Vehicle. In such cases, the same initialization mechanism will be followed. The initialization chain finally leads to the java.lang.Object class as all the classes in Java implicitly inherits from the Object class.

The important thing is however the order of initialization. According to Java Language Specification, the initialization starts with super class fields and end with the fields declared in the Object’s own class. Thus, in our previous example, the order of initialization will be:

  1. All variables in the Object class. (No fields declared)
  2. All variables in the Vehicle class. (field registration).
  3. All variables in the Car class (field model).

The logic behind this order of initialization is to ensure that you can use a super class variable within a sub class initialization properly.

The Initialization Mechanism…

In Java, the initialization of objects can be done in two ways:

  • The static initialization blocks
  • The constructor

The static initialization block is invoked only when the Class Loader in Java loads the class. The constructor is not invoked unless you explicitly invoke them in order to initialize an instance of an Object.

In Java the first thing that a constructor does is to invoke another constructor in the super class. In order to fully understand this, I would try to iterate the mechanism in the following points to note.

  • In Java if no constructor is explicitly defined for a class, the compiler provides a default no-argument constructor. However, if you explicitly specify a public constructor, compiler will not place any default constructor.
  • When the constructor of the sub class is invoked, at first the compiler will invoke the default constructor in the super class recursively up in the tree.
  • The super class’s constructor can also be invoked explicitly by using the super() key word.
  • From within a constructor, you are free to call a super class constructor with any number of arguments. It does not have to be the default constructor only.
  • It is important however to note that the order of constructor invocation starts from the Object’s class and proceeds recursively up in the tree. However, if you remember that the order of instance variable initialization proceeds from the base class down to the Object’s class. (Figure 1).
  • The previous point coincides with the fact that eventually before a constructor of the Object’s class has finished its job, a constructor of the super class is always called, which will initialize the super class fields.

Inheritance Image
Figure 1: The Constructor invocation and Object initialisation order

Now that we understand how the inheritance in Java really happens behind the scene, it is time to think more about the implications. At first sight, inheritance is an attractive mechanism. But on second thoughts, we will reveal some of the problems that might arise with the user of inheritance.

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.