Subscribe Us

Inheritance In Java

.
Inheritance In Java
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.


extend Keyword : By using the extends keyword we inherit the class properties .extends keyword provide the relationship between parent and child class.
Super Class or Parent Class : Super class is a class whose properties is inherited . Super class is also know as the bases class.
Sub Class or Child Class : Sub class is a class which inherit the properties of other class.Sub class is also known as the derived class or extend class
Reusability : The main objective of inheritance is to reuse the code , whenever we extend on class to another then the code reused automaticlly. 

Example of Inheritance 
Syntax
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
Inheritance in java
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.
5) Hybrid Inheritance.

Single Inheritance in Java

In below image the class A is a parent and Class B is as child class.

Single Inheritance in Java
Example of single inheritance 
Syntax
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);  
}  
}
OutputEmployee ID is :001
            Employee floor is :70

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

Multilevel Inheritance in java

Example of Multilevel inheritance 
Syntax
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 same class then this type of inheritance is called as the hierarchical inheritance .In below diagram class 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 
Syntax
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 inherit the feature of more than one parent class then it is the concept of multiple inheritance .here in below diagram class C Inherit the class A and Class B .
Java does not support the multiple inheritance concept with classes .we can achieve the multiple inheritance by interface.

Multiple Inheritance in java

Example of multiple inheritance 
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 extends the Class C ,and another Class D extend 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

-----------------------The End ---------------------------

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

Thank You !
Sandeep Yadav

Tags: Inheritance in java , single inheritance in java , Multilevel inheritance in java , java inheritance ,types of inheritance in java multiple inheritance in java

Post a comment

0 Comments