Inheritance In Java - Why use Inheritance?

Inheritance is an important concept of OOP (object-oriented programming ). Inheritance is a process where one class acquire the properties ( fields and methods ) of another class. Inheritance is also known as the IS-A relationship.

The main objective of java inheritance is to reuse the code.

Inheritance In Java with Example



Extend Keyword: By using the extends keyword we inherit the class properties .extends keyword provides the relationship between parent and child class.

Super Class or Parent Class: A superclass is a class whose properties are inherited. The superclass is also known as the bases class.

Sub Class or Child Class: A subclass is a class which inherits the properties of another class. The subclass is also known as the derived class or extended class

Reusability: The main objective of inheritance is to reuse the code, whenever we extend one class to another then the code is reused automatically. 

Example of Inheritance 
An example of inheritance is given below.


class Employee
{  
 int empId=001;  
}  
class Company extends Employee
{  
int floor=70;  
public static void main(String args[])
{  
Company c=new Company();  
System.out.println("Employee ID is:"+c.empId);  
System.out.println("Employee floor is:"+c.floor);  
}  
}


Output: 
Employee ID is:001
Employee floor is:70

Types of Inheritance in java
In java programming, there are 5 types of inheritance.

1) Single Inheritance.

2) Multilevel Inheritance.
3) Hierarchical Inheritance.
4) Multiple Inheritance. (Not supported by java)
5) Hybrid Inheritance. (Not supported by java)

Single Inheritance in Java
In Java when a class inherits the property of the other class then it is called single inheritance in java.
In the below image, class A is a parent and Class B is a child class.

Single Inheritance in Java

Example of Single Inheritance 
An example of single inheritance is given below.


class Employee
{  
 int empId=001;  
}  
class Company extends Employee
{  
int floor=70;  
public static void main(String args[])
{  
Company c=new Company();  
System.out.println("Employee ID is:"+c.empId);  
 System.out.println("Employee floor is:"+c.floor);  
}  
}

Output: 
Employee ID is:001
Employee floor is:70

Multilevel Inheritance in Java 
When a class inherit the feature of a class, which inherits the feature of another class then this is called multilevel inheritance.
In the below image Class C extends class B and Class B extends Class A. This is how multilevel inheritance work.


Multilevel Inheritance in java

Example of Multilevel Inheritance 
An Example of Multilevel inheritance is given below.

class Car
{  
void carSpeed()
{
System.out.println("Car speed is 120kmph");
}  
}  
class Alto extends Car
{  
void altoSpeed()
{
System.out.println("Alto speed is 100kmph");
}  
}  
class Nano extends Alto
{  
void nanoSped()
{System.out.println("Nano speed is 60 kmph");
}  
}  
class SpeedCheck
{  
public static void main(String args[])
{  
Nano s=new Nano();  
d.nanoSpeed();  
d.altoSpeed();  
d.carSpeed();  

}
}

Output 
Nano speed is 60 kmph
Alto speed is 100kmph
Car speed is 120kmph

Hierarchical Inheritance in Java
In java, When more than one Class inherit the feature of the same class then this type of inheritance is called hierarchical inheritance. In the below diagram classes B, C and D inherit the feature of class A. Here, Class A is the parent class for Class B, C and D.


Hierarchical Inheritance 

Example of hierarchical Inheritance 

class Car
{  
void carSpeed()
{
System.out.println("Car speed is 120kmph");
}  
}  
class Alto extends Car
{  
void altoSpeed()
{
System.out.println("Alto speed is 100kmph");
}  
}  
class Nano extends Car
{  
void nanoSped()
{System.out.println("Nano speed is 60 kmph");
}  
}  
class SpeedCheck
{  
public static void main(String args[])
{  
Nano s=new Nano();  
d.nanoSpeed();  
d.carSpeed();  
}
}

output:
Nano speed is 60 kmph
Car speed is 120kmph

Multiple Inheritance
When a subclass inherits the feature of more than one parent class then it is the concept of multiple inheritances .here in the below diagram class C Inherit class A and Class B.
Java does not support the multiple inheritance concept with classes. we can achieve multiple inheritances by the interface.

Multiple Inheritance in java

Example of multiple Inheritance
A basic example of multiple Inheritance is given below.

class Teacher
{  
void homework()
{
System.out.println("Essay on Cow");
}  
}  
class Student
{  
void homework()
{
System.out.println("how to write");
}  
}  
class Hod extends Teacher,Student
{ 
   
 public static void main(String args[]){  
   Hod ess=new Hod();  
   ess.homework();
}  

}

Output
Compile Time Error

Hybrid Inheritance 
In java, Hybrid inheritance is a combination of two or more types of inheritance. if a class A and Class B extend Class C, and another Class D extends Class A, this is how hybrid inheritance work.java does not support the hybrid inheritance with classes. we can achieve the hybrid inheritance by Interface.


Hybrid Inheritance In Java

Importance of Inheritance in Java
Some of the important inheritance in java are listed below.
  • Reusability of code
  • Saving time and resources
  • creating better connections between different classes
  • achieving method overriding. 


Hope !!! The above Lesson " Inheritance in java "is helpful for you ...

Technology Team, 

UPCS 
UP Consultancy Services 

TagsInheritance in java, single inheritance in java, Multilevel inheritance in java, java inheritance, types of inheritance in java , multiple inheritances in java