In this article, we are going to cover one famous feature of Java, that is inheritance. If you ask someone, what is OOP? They will no doubt say something about inheritance and class hierarchy. Of course, this is just one feature of object-oriented concepts, but it’s an important one. We’re going to find out what it is!
What is Inheritance?
Inheritance is a code reuse technique in which one class inherits, or copies, methods and member variables of another class. As an example, consider that we have two classes
Square as shown in the figure below. The
Rectangle class has two
protected member variables
area() that computes the area, and one
Conceptually a square is a rectangle, and the logic to compute the area is the same. In this case, it make sense for the
Square class to inherit the
Rectangle class and reuse the
area() method. The figure below shows the inheritance relationship, which denoted by arrow.
In Java, inheritance is done using the
**extends** keyword, as shown in the code below.
In the code above,
Square (the child class), extends the
Rectange (the parent or super class); so the
Square gets the
area() method without any code. Note that the
Driver class is able to call the
area() method of the
What is Inherited
Java allows what is inherited by a child class, based on the visibility modifiers. The following table summarizes what is inherited based on modifier.
|Visibility||Inherited by Child||Accessible by Another Class|
|Class of same package||Class of same Package|
As one can see, public and protected members are inherited by child class; whereas private members are not not.
final methods are also inherited, when visibility allows.
|Modifier||Inherited by Child|
The following example shows inheritance of a
static final method.
As one can imagine, public and protected constructors are also inherited. However, they are not available when constructing the child class. For example, the following code will produce compiling error.
Although, the constructor of the
Rectangle class is public and in theory inherited by the
Square class, it cannot be used when instantiating the
Square class. The reason for this is that, constructors have the same name as the class. When we invoke
new Square(), we are instructing Java to look for a method named
Square(), which excludes the
Rectangle constructor, named
Nonetheless, child class is able to access constructors of parent class. As shown in the first code example, parent constructors can be called using
**super()**, which is a reference to the parent class in Java. As expected, based on the parameters provided, Java will call the correct constructor.
One thing to note is that, if used,
super() has to be the first statement in the child constructor. If the constructor needs to do something before calling the parent’s constructor, consider using initialization methods.
The Object Class
Object class is the root of Java’s class hierarchy. Every class in Java is a descendant of the
Object class and inherit from it. If a class does not explicitly inherit a class using
extends, then it is a direct child of the
Object class. In other words, every class in Java has a parent, except for the
Object class defines the most generic skeleton methods that make Java work. For a list of methods defined by this class, see Object API.
A class that is marked with the
final modifier cannot be inherited. The following example produces a compiling error, because class
Java Class Hierarchy and Class Library
The Java class library is a large hierarchy of classes that can be used by programmers. The root of this hierarchy of course is the
Object class. For example, the following figure shows just a few classes in this hierarchy.
Programmers can add to this hierarchy by writing classes that extending classes in the hierarchy.