Java Programming Tutorial Index
Java Object Oriented

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. This tutorial will teach you about inheritance and its uses and types.

What is Inheritance?

Inheritance is the procedure or mechanism of acquiring all the properties and behavior of one class to another, i.e., acquiring the properties and behavior of a 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. It is mainly used for code reusability within a Java program.

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

Syntax:

class base 
{
   .....
   .....
}
class derive extends base
 {
   .....
   .....
}

Types of Inheritance

Let's 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, more than one derived class is created from one 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
 }
}

Inheritance in Java - Video Tutorial

Please watch this video tutorial to understand "Java Inheritance" in more depth.

Why does Java not provide multiple inheritances?

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

So Java reduces this hectic situation by using interfaces that implement this concept and reduce this problem, as compile-time errors are more tolerable than runtime faults in the program.