Inheritance in java
previous NextInheritance is an important part of oops in Java. Because inheritance is such a mechanism by which a child class is created from a parent class. Through this, the properties of the parent class can be used in the child class. To use the properties of the parent class in the child class, the parent class has to be inherited. And to inherit the parent class, the derivatives public, private, and protected are used. And the extends keyword is used to inherit the attribute of the parent class in the child class. In inheritance, the parent class is called the base class or superclass, the class from which the property is taken. And the new class is called child class or derived class or subclass, by which the property is taken.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Why use inheritance in java
- For Method Overriding (so runtime polymorphism can be achieved).
- For Code Reusability.
- There is no need to write much code, which saves time.
Syntax
class subclass-name extends Superclass-name { //methods and fields }
Example
class Employee {
float salary=40000f;
}
class Programmer extends Employee{
int fund=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Employee salary is:"+p.salary);
System.out.println("fund of Programmer is:"+p.fund);
}
}
Output :
Employee salary is:40000.0 fund of programmer is:10000
Types of Inheritance in java
- Single inheritance
- Multiple inheritances
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
Single inheritance in java
When one class is inherited by another class then that inheritance is called single inheritance. In this, the property of one class is taken by another class. In this there is only one superclass and one subclass.
Syntax
class A{ } class B extends A{ }
Example
class Single_inheritance{
static String name = "Single inheritance";
public void parent(){
System.out.println("parent class method");
}
}
class B extends Single_inheritance{
public void child(){
System.out.println("child class method");
}
public static void main(String[] args) {
B obj = new B();
obj.child();
System.out.println("parent class variabe = "+ name);
}
}
Output :
child class method parent class variabe = Single inheritance
Multiple inheritance in java
When more than one class is inherited by one class, that inheritance is called multiple inheritance. In this, the property of more than one class is inherited by one class. In this there is more than one super class and one sub class. Java does not support multiple inheritance.
Syntax
class A{ } class B{ } class c extends A,B{ }
Example
class Multiple_inheritance1{
public void parent(){
System.out.println("parent class method 1");
}
}
class Multiple_inheritance2{
public void method(){
System.out.println("parent class method 2");
}
}
class Example extends Multiple_inheritance1,Multiple_inheritance2{
public void child(){
System.out.println("child class method");
}
public static void main(String[] args) {
Example obj = new Example();
obj.parent();
}
}
Output :
C:\Users\a\Desktop>javac Multiple_inheritance1.java Multiple_inheritance1.java:13: error: '{' expected class Example extends Single_inheritance,Multiple_inheritance2{ ^ 1 error
Multilevel inheritance in java
When more than one class inherits each other in one level then that inheritance is called multilevel inheritance. In this, a class inherits another class and the class that inherits the class becomes a sub class and the same sub class is inherited by another class. Similarly all classes inherit each other.
Syntax
class A{ } class B extends A{ } class c extends B{ }
Example
class Animal{
void eat(){
System.out.println("eating...");
}
}
class Dog extends Animal{
void bark(){
System.out.println("barking...");
}
}
class Pats extends Dog{
void weep(){
System.out.println("weeping...");
}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output :
weeping... barking... eating...
![](https://www.learnhindituts.com/assets/images/learnhindituts-learn-tutorials-in-hindi.png)
Hierarchical inheritance in java
When a base class is inherited by more than one sub-class, that inheritance is called hierarchical inheritance. In this, the property of one class is taken by more than one class. In hierarchical inheritance, there is one base class and more than one sub-class. This is the opposite of multiple inheritances.
Syntax
class A{ } class B extends A{ } class c extends A{ } class D extends c{ }
Hybrid inheritance in java
When we mix any two types of inheritance in our program, it is called hybrid inheritance. This inheritance is a combination of more than one inheritance. That is, it is made up of two or more inheritances.
java does not support Hybrid inheritance.