Java Inheritance

Inheritance is one of the significant features of an object-oriented programming language. This is a special feature as it reduces programmers re-writing effort. In this tutorial, you will be learning about inheritance and its uses and types.

What is Inheritance?

Inheritance can be defined as the procedure or mechanism of acquiring all the properties and behavior of one class to another, i.e., acquiring the properties and behavior of child class from the parent class. This concept was built to achieve the advantage of creating a new class that gets built upon an already existing class(es). It is mainly used for code reusability within a Java program.

Moreover, a hierarchical order of management of information can also be done using this concept. Here two types of classes build relationships with each other which are a base class and derived class. The class that gets inherited taking the properties of another class is the subclass or derived class or child class. Again, the class whose properties get inherited is the superclass or base class or parent class. The keyword extends used to inherit the properties of the base class to derived class. The structure of using this keyword looks something like this:

Syntax:
class base 
{
   .....
   .....
}
class derive extends base
 {
   .....
   .....
}

Types of Inheritance

Lets now discuss the various types of inheritance supported by Java. Here's a block-diagram of three inheritances.

Java supports three types of inheritance. These are:

Single Inheritance

When a single class gets derived from its base class, then this type of inheritance is termed as single inheritance. The figure drawn above has class A as the base class, and class B gets derived from that base class.

Example:
class Teacher {
 void teach() {
  System.out.println("Teaching subjects");
 }
}
class Students extends Teacher {
 void listen() {
  System.out.println("Listening to teacher");
 }
}
class CheckForInheritance {
 public static void main(String args[]) {
  Students s1 = new Students();
  s1.teach();
  s1.listen();
 }
}

Multi-level Inheritance

In this type of inheritance, a derived class gets created from another derived class and can have any number of levels.

Example:
class Teacher {
 void teach() {
  System.out.println("Teaching subject");
 }
}
class Student extends Teacher {
 void listen() {
  System.out.println("Listening");
 }
}
class homeTution extends Student {
 void explains() {
  System.out.println("Does homework");
 }
}
class CheckForInheritance {
 public static void main(String argu[]) {
  homeTution h = new himeTution();
  h.explains();
  d.teach();
  d.listen();
 }
}

Hierarchical Inheritance

In this type of inheritance, there are more than one derived classes which get created from one single base class.

Example:
class Teacher {
 void teach() {
  System.out.println("Teaching subject");
 }
}
class Student extends Teacher {
 void listen() {
  System.out.println("Listening");
 }
}
class Principal extends Teacher {
 void evaluate() {
  System.out.println("Evaluating");
 }
}
class CheckForInheritance {
 public static void main(String argu[]) {
  Principal p = new Principal();
  p.evaluate();
  p.teach();
  // p.listen(); will produce an error
 }
}

Why does Java not provide multiple inheritances?

Let us imagine a situation where there are three classes: A, B and C. The C class inherits A and B classes. In case, class A and class B have a method with same name and type, and as a programmer, you have to call that method from child class's (C) object, there-there will be ambiguity as which method will be called either of A or B class.

So Java reduces this hectic situation by the use of interfaces which implements this concept and reduce this problem; as compile-time errors are tolerable than runtime faults in the program.


Scroll Back to Top