The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Inheritance in Java

Inheritance is an important concept in Java which allows one class to derive the features of another class. This supports code reusability and save implementation time. The existing class is known as super class ( base
class ) and the class which inherits the data members and methods of the super class is known as sub class
( derived class ). The use of inheritance reflects is-a relationship between two entities (classes). Suppose we have an existing class fruit and we want to create a new class apple. Now, apple is a fruit. So, apple class can inherit features from fruit class. Subclass inherits features from Super Class using extends keyword.

Forms of Inheritance
Single Inheritance : It is the simplest form of inheritance involving one super class and one subclass. Following program is a simple example of simple inheritance :

/* define the super class 'X' */
class X {
   protected int a; // variable 'a' is visible to subclass
   X() { }
   X(int val) {
        a = val;
   }
   public void increment() {
      a++;
   }
}

/* define the subclass 'Y' which inherits from 'X'
 * variable 'a' and method 'increment' is reflected in 'Y'
 */
class Y extends X {
   int b, c;
   Y() { }
   Y(int val1, int val2, int val3) {
      super(val1); // call the constructor of super class
      b = val2; c = val3;
   }
   void display() {
      System.out.println("a = " + a + " b = " + b + " c = " + c);
   }
}

public class SingleInheritanceDemo {
   public static void main(String args[]) {
      Y obj = new Y(3, 8, 5);
      obj.increment();
      obj.display();
   }
}

Multilevel Inheritance : Consider a base class A and another class B which inherits from A. Now another class C inherits from B. So, B becomes the subclass for C and super class for A. This kind of inheritance is known as multilevel inheritance. See the program below :

class X {
   protected int a;
   X() { }
   X(int val) {
        a = val;
   }
   public void incrementX() {
      a++;
   }
}

/* Variables and methods of 'X' are reflected in Y */
class Y extends X {
   protected int b;
   Y() { }
   Y(int val1, int val2) {
      super(val1);
      b = val2;
   }
   public void incrementY() {
      b++;
   }
}

/* Variables and methods of 'X' and 'Y' are reflected in Z */
class Z extends Y {
   protected int c;
   Z() { }
   Z(int val1, int val2, int val3) {
      super(val1, val2);
      c = val3;
   }
   public void incrementZ() {
      incrementX();
      incrementY();
      c++;
   }
   void display() {
      System.out.println("a = " + a + " b = " + b + " c = " + c);
   }
}

public class MultilevelInheritanceDemo {
   public static void main(String args[]) {
      Z obj = new Z(3, 8, 5);
      obj.incrementZ();
      obj.display();
   }
}

Hierarchical Inheritance : In this form of inheritance multiple classes inherits from a single class i.e there is one super class and multiple subclasses. This form of inheritance is commonly used in Java program design. Following program illustrates the concept :

class Fruit {
   protected void fruitInfo() {
      System.out.println("I am a fruit. ");
   }
}

// class 'Mango' inherits from class 'Fruit'
class Mango extends Fruit {
   public void mangoInfo() {
      fruitInfo(); // calling base class function
      System.out.println("My name is mango. ");
   }
}

// class 'Apple' inherits from class 'Fruit'
class Apple extends Fruit {
   public void appleInfo() {
      fruitInfo();// calling base class function
      System.out.println("My name is apple. ");
   }
}

public class HierarchicalInheritanceDemo {
   public static void main(String args[]) {
      Mango m = new Mango();
      Apple a = new Apple();
      m.mangoInfo();
      a.appleInfo();
   }
}

Java has no support for Multiple ( one subclass inheriting from multiple super classes ) and Hybrid ( combination of two or more forms ) inheritance forms to keep the language simple. However to overcome the limitation, Java introduced the concept of interfaces which we will discuss later in the tutorial.

instanceof keyword
We have discussed instanceof keyword previously but now we show the actual use case which comes into play when a Java based system is designed with inheritance and at some point we want to know the super class of a particular class. Following program illustrates this concept :

class Fruit { }
class Mango extends Fruit { }

public class InstanceofDemo {
   public static void main(String args[]) {
      Mango m = new Mango();
      boolean x = m instanceof Mango; // returns true
      boolean y = m instanceof Fruit; // returns true
      System.out.println("x = " + x + " and y = " + y);
   }
}

Final Methods
If we use final modifier with a method, then that method can not be overridden in the subclasses. The main purpose of final method is to prevent modification of the method behaviour by other classes.

Final Classes
If we do not want a class to be subclassed for security reasons or unwanted extensions, then we need to declare that class as final using the final modifier. No class can inherit any property of a final class.

Abstract Methods and Classes
A class is declared as abstract when it is meant to be inherited. An abstract class by itself can never be instantiated. An abstract method is a method declared without any body / implementation. The method body must be implemented by the subclass. Any class containing abstract method must also be abstract and if the subclass doesn't implement the abstract method of super class, then the subclass should also be declared as abstract. Following program demonstrates abstract classes and methods :

/* Abstract class */
abstract class Fruit {
   // abstract method ( will be defined in subclass )
   abstract void fruitInfo();
}

class Mango extends Fruit {
   void fruitInfo() {
      System.out.println("My name is Mango. I am a fruit");
   }
}

class Apple extends Fruit {
   void fruitInfo() {
      System.out.println("My name is Apple. I am a fruit");
   }
}

public class AbstractDemo {
   public static void main(String args[]) {
      Mango m = new Mango();
      Apple a = new Apple();
      m.fruitInfo();
      a.fruitInfo();
   }
}

Back | Next