Java Inheritance
Java Inheritance defines an IS-A relationship between a superclass and its subclasses. This means that an object of a subclass can be used wherever an object of the superclass can be used. Inheritance in Java mechanism is used to build new classes from existing classes. The inheritance relationship is transitive: if class X extends class Y, then a class Z, which extends class x, will also inherit from class Y.
Example 1
For example the Volume class can inherit some properties from the Area class. Here we find that the superclass is the Area class and the subclass is the Volume class. A subclass must use the “extends” clause to derive from a superclass which must be written in the header of the subclass definition. The subclass inherits members of the superclass and hence promotes code reusability. The subclass itself can add its own new behavior and properties. The java.lang.Object class is always at the top of any class inheritance hierarchy. See the program below.
InheritanceTest.java
// Example code to demonstrate inheritance
class Area {
int length, breadth; //instance variables
Area(int length, int breadth) { //local variables
//'this' is the reference of the current class
this.length = length;
this.breadth = breadth;
}
int computeArea() {
return length * breadth;
}
}
class Volume extends Area {
int height;
Volume(int length, int breadth, int height) {
super(length, breadth); //call to the super class constructor
this.height = height;
}
int computeVolume() {
return length * breadth * height;
}
}
public class InheritanceTest {
public static void main(String[] args) {
Area ac = new Area(15, 12); //object of superclass
System.out.println("AREA1 = " + ac.computeArea() + " square units.");
Volume vc = new Volume(20, 15, 12); //object of subclass
System.out.println("AREA2 = " + vc.computeArea() + " square units.");
System.out.println("VOLUME = " + vc.computeVolume() + " cube units.");
}
}
Output:
AREA1 = 180 square units.
AREA2 = 300 square units.
VOLUME = 3600 cube units.
Types of Inheritance
There are five types of inheritance. Of them Java supports the first three only.
- Single inheritance – Class B extends from class A only.
- Multilevel inheritance – Class B extends from class A; then class C extends from class B.
- Hierarchical inheritance – Class A acts as the superclass for classes B, C, and D.
- Multiple inheritance – Class C extends from interfaces A and B.
- Hybrid inheritance – Mix of two or more types of inheritance.
Java doesn’t support multiple and hybrid inheritance through classes. However, we can achieve multiple inheritance in Java through interfaces. We will learn about interfaces in later chapters.
IS-A Relationship
IS-A is a way of specifying: this object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.
class Animal { } class Mammal extends Animal { } class Lion extends Mammal { }
Now, based on the above example, in OOP terms, the following are true −
- Animal is the superclass of Mammal class.
- Mammal is a subclass of Animal class.
- Lion is the subclass of both Mammal and Animal classes.
Now, if we consider the IS-A relationship, we can say −
- Mammal IS-A Animal
- Lion IS-A Mammal
- Hence Lion IS-A Animal as well
With the use of the extends keyword, the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.
Inheritance and Constructor (w.r.t. throws clause with exceptions)
The rule for using “throws clause with exceptions” specifies that a subclass constructor can allow more, but not less, exceptions than the superclass constructor. For example, an exception associated with subclass constructor in throws clause can be same or supertype, but not subtype, in the superclass. Doing so, will generate compile time error.
Example 2
See the example below.
ConstructorException.java
import java.io.IOException;
class Super {
Super() throws IOException {
System.out.println("Super class constructor");
}
}
class Sub extends Super {
Sub() throws Exception {
System.out.println("Sub class constructor");
}
}
public class ConstructorException {
public static void main(String[] args) throws Exception {
Super s; //superclass reference
s = new Super(); //object of superclass
s = new Sub(); //object of subclass
}
}
NOTE :- The java.lang.Exception class is a superclass of java.io.IOException class. See exception handling topic for more details.