Types of Inheritance In java with Example

Inheritance in Java

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.

Inheritance represents the IS-A relationship, also known as parent-child relationship.

Why use inheritance in java

  • For Method Overriding (so runtime polymorphism can be achieved).
  • For Code Reusability.

(Note: Java doesnot support Multiple Inheritence due to security reason But we achieve it by using a new concept called Interface)

inheritance_thumb[5]
Java Inheritance

Syntax of Java Inheritance:

 

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

The extends keyword indicates that you are making a new class that derives from an existing class.

In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.



 

Single Inharitence

Single Inheritance is method in which a derived class has only one base class.

An example of single Inheritance:

 

public class Inherit_Single {
	
	protected String str;
	
	Inherit_Single() {
		
		str = "Java ";
	}
}
 
class SubClass extends Inherit_Single {
	
	SubClass() {
	
		str = str.concat("World !!!");
	}
	
	void display()
	{
		
		System.out.println(str);
	}
}
 
class MainClass {
	
	public static void main (String args[]){
		
		SubClass obj = new SubClass();
		
		obj.display();
	}
}

Sample Output

 

Java World !!!

Multi-level Inheritance in Java

Multi-Level Inheritance Says that there would be two or more class in the parent. 1st class will be extends in the second class and the second will be extends in third class. and so on.
multi level inheritance

 

An example of Multi-Level Inheritance.

class Car{
	public Car()
	{
		System.out.println("Class Car");
	}
	public void vehicleType()
	{
		System.out.println("Vehicle Type: Car");
	}
}
class Maruti extends Car{
	public Maruti()
	{
		System.out.println("Class Maruti");
	}
	public void brand()
	{
		System.out.println("Brand: Maruti");
	}
	public void speed()
	{
		System.out.println("Max: 90Kmph");
	}
}
public class Maruti800 extends Maruti{

	 public Maruti800()
	 {
		 System.out.println("Maruti Model: 800");
	 }
	 public void speed()
		{
			System.out.println("Max: 80Kmph");
		}
	 public static void main(String args[])
	 {
		 Maruti800 obj=new Maruti800();
		 obj.vehicleType();
		 obj.brand();
		 obj.speed();
	 }
}

Output:

Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph








 

Hierarchical Inheritance

In Hierarchical Inheritance there would be on parent class whic will extends in two or more classes as shown in diagram.

download

An example of Hierarchical Inheritance.

Class A
{
  public void methodA()
  {
     System.out.println("method of Class A");
  }
}
Class B extends A
{
  public void methodB()
  {
     System.out.println("method of Class B");
  }
}
Class C extends A
{
 public void methodC()
 {
 System.out.println("method of Class C");
 }
}
Class D extends A
{
  public void methodD()
  {
     System.out.println("method of Class D");
  }
}
Class MyClass
{
  public void methodB()
  {
     System.out.println("method of Class B");
  }
  public static void main(String args[])
  {
     B obj1 = new B();
     C obj2 = new C();
     D obj3 = new D();
     obj1.methodA();
     obj2.methodA();
     obj3.methodA();
  }
}

The above would run perfectly fine with no errors and the output would be –

method of Class A
method of Class A
method of Class A