Inheritance in Java

Table of Contents

In general, the meaning of Inheritance stands for the absorption of the characters passed by the parents to the progeny or siblings. In a similar fashion, the transfer of the characters from the superclass to the subclass in Java is known as Inheritance.

Let’s see a basic example to get gist of the syntax .

class Simple//Super Class
{  
 int a=10;  
}  
class Sample extends Simple//Sample is the subclass here
{  
 int b=20;  
 public static void main(String args[])
 {  
   Sample obj=new Sample();  
   System.out.println(obj.a);  
   System.out.println(obj.b);  
 }  
}  

Now, if you look into the above example, then you can notice that to inherit all the methods or variables of the Superclass, which is Simple, needed to be appended after the name of the Subclass, which is Sample, with a keyword just before of it which is extends. What it means that it actually is extending the above class by just using the extends keyword.

Now here is the list of various types of Inheritance strategy which is used in Java:

Single Inheritance

When a class extends another class, it is known as Single Inheritance. For example, if a Car is a Super Class, then a Sports Car is a subclass of it, where all the basic features of a Sports Car like Chesis, Windshield, Wheels, Steering, Brakes, etc is actually inherited from its parent class which is a Car. Now over that basic configuration, the Sports Car is compiled of some advanced features like four-wheel drive, Turbo engine, ABS system, Cockpit ejection, Air Bags.

class Simple//Super Class
{  
 int a=10;  
}  
class Sample extends Simple//Sample is the subclass here
{  
 int b=20;  
 public static void main(String args[])
 {  
   Sample obj=new Sample();  
   System.out.println(obj.a);  
   System.out.println(obj.b);  
 }  
}  

Multilevel Inheritance

A chain of inheritance, where suppose Class A(Super Class) is inherited by Class B(Sub Class) and again Class B as Super Class is inherited by Class C which will be a Subclass.

class First
{  
void meth1(){System.out.println("First");}  
}  
class Second extends First
{  
void meth2(){System.out.println("Second");}  
}  
class Third extends Second
{  
void meth3(){System.out.println("Third");}  
}  
class Sample
{  
   public static void main(String args[])
   {  
   Third a=new Third();  
   a.meth1();  
   a.meth2();  
   a.meth3();  
   }
} 

Hierarchical Inheritance

When a Super class suppose Class A is inherited by more than one Sub-classes like Class B and Class C,then this will be known as Hierarchical Inheritance.

class First
{  
void meth1(){System.out.println("First");}  
}  
class Second extends First
{  
void meth2(){System.out.println("Second");}  
}  
class Third extends First
{  
void meth3(){System.out.println("Third");}  
}  
class Sample
{  
   public static void main(String args[])
   {  
   Third a=new Third();  
   a.meth1();  //No error
   a.meth2();  //Error 
   a.meth3();  //No error
   }
} 

Now, in the above example, you will get an error, which is because you have created an object of Class Third, but you are also trying to access the elements of Second.

Why Multiple Inheritance and Hybrid Inheritance, is not supported in java?

Because of ambiguity and since Java considers Compile time errors better than Runtime error,hence implementing of Multiple Inheritance is not done like this but it is possible through Data Abstraction, which we will learn later.

Previous This Keyword in Java
Next Interface in Java

Leave a Reply