Inheritance in Java

Inheritance is a way where one class acquires all the property and behaviour of the parent class. And Inheritance is used to achieve dynamic binding and code reusability in java. In java two ways to achieve inheritance.

  • IS-A Relationship
  • HAS-A Relationship

Types of Inheritance:-

  1. Single Inheritance:- In the Single Inheritance, one child class inherit property and behaviour of a parent class.
single inheritance
single inheritance

2. Multilevel Inheritance:- In the multilevel Inheritance child class B inherited the property and behaviour of parent class A, and then child class C inherited the property and behaviour of parent class A and class B.

multilevel inheritance

3. Hierarchical Inheritance:- In the hierarchical inheritance, multiple child classes inherit a parent class property and behaviour.

 inheritance

Note:- Java does not support multiple and hybrid inheritance in the case of classes. It is supported only in the case of the interface.

4. Multiple Inheritance:- In the multiple inheritances, a child class inherit multiple classes. But Java does not support multiple inheritances in case of only classes, not interfaces.

multiple

5. Hybrid Inheritance:- In java it’s supported in the combination of interfaces and classes.

hybrid

Note:- In java extends keyword is used to inherit the other class.

It is an IS-A Relationship, here you can easily understand the IS-A relationship.

Single Inheritance Example:- In this inheritance Child class inherit a Parent class execute data member and member function after creating the Child class (instance) object.

//Single Inheritance Example
class Parent
{
public void show()
{
System.out.println("Parent class"); 
}
}

class Child extends Parent
{
public void newShow()
{
System.out.println("Child class");
}
} 

class SingleInheritance
{
public static void main(String args[])
{
Child c=new Child();
c.show();
c.newShow();
}
} 

Multilevel Inheritance example:-

class Parent
{
public void show()
{
System.out.println("Parent class"); 
}
}

class Child extends Parent
{
public void childShow()
{
System.out.println("Child class");
}
}

class Child1 extends Child
{
public void child1Show()
{
System.out.println("Child1 class");
}
}  

class MultilevelInheritance
{
public static void main(String args[])
{
Child c=new Child();
c.show();
c.childShow();
c.child1Show();
}
} 

Hierarchical Inheritance:- In this inheritance, any class inherit a Parent class and parent all the method available in the Child class. And you can execute parent class method creating child class instance (Object).

class Parent
{
public void show()
{
System.out.println("Parent class"); 
}
}

class Child extends Parent
{
public void childShow()
{
System.out.println("Child class");
}
public static void main(String args[])
{
Child c=new Child();
c.show();
c.childShow();
} 
}

class Child1 extends Parent
{
public void child1Show()
{
System.out.println("Child1 class");
}
public static void main(String args[])
{
Child c=new Child();
c.show();
c.child1Show(); 
}
}  

HAS-A Relationship or Aggregation:-

If a class have the other class reference (object). It is known as aggregation. It is another way to achieve dynamic binding in java. In the aggregation, a class have the other class Reference ID (RID) and you have used this reference id and access the all the data member and member function on these class.

class SmartPhone
 {
 public void condition()
 {
 System.out.println("Condition : New");
 }
 }

 class SmartPhoneDetails
 {
 SmartPhone sp;//aggregation or has-a relationship
 public void details(String model,int ram,String processor)
 {
 sp=new SmartPhone();
 System.out.println("Model Name : "+model);
 System.out.println("RAM : "+ram+"GB");
 System.out.println("Processor : "+processor);
 sp.condition();
 }
 public static void main(String args[])
 {
 SmartPhoneDetails l=new  SmartPhoneDetails();
 l.details("Redmi Note 5 Pro",4,"Snapdragon 636");
 }
 }
aggregation answer

4 thoughts on “Inheritance in Java – All You Need to Know

  1. Hello, I would like to appreciate your work. It’s a great platform got to learn a lot. With your help I had created backlinks for my website. Your data is really worthy. Thank you so much. Keep it up!

Leave a Reply

Your email address will not be published. Required fields are marked *