Google News
logo
Java Inheritance
Inheritance:-
The process of getting properties and behaviors from one class to another class is called inheritance.
Properties : variables
Behaviors : methods
1. The main purpose of the inheritance is code extensibility whenever we are extending automatically the code is reused.
2. In inheritance one class giving the properties and behavior and another class is taking the properties and behavior.
3. Inheritance is also known as is-a relationship means two classes are belongs to the same hierarchy.
4. By using extends keyword we are achieving inheritance concept.
5. In the inheritance the person who is giving the properties is called parent the person who is taking the properties is called child.
6. To reduce length of the code and redundancy of the code sun peoples introducing inheritance concept.
Java Images
Types of inheritance in java
1. Single inheritance: If one class properties are inherited into another single class is known as Simple or Single Inheritance Inheritance flow for the below figure can be represented as: A =>B
Java Images
Single inheritance program
class aa2{
int a; 
void get(int a){ 
this.a=a; 
} 

} 

public class bb extends aa2{ 
int b; 
void add(int b){ 
this.b=b; 
System.out.println(a+b); 
} 
public static void main(String ba[]){ 
bb d=new bb(); 
d.get(10); 
d.add(20); 
} 

}
Output :
output:30
Multilevel inheritance:-
Whenever multiple classes are participating in inheritance in sequential order, then such inheritance is called as “Multi-level Inheritance”.
Java Images
Multilevel inheritance program
class supper1{ 
int a=10; 
} 
class supper2 extends supper1{ 
int b=20; 
} 
public class sup extends supper2{ 

void add(){ 
System.out.println(a+b); 
} 
public static void main(String abc[]){ 
sup s=new sup(); 
s.add(); 
} 

}
Output :
output:30
3. Hierarchical inheritance
Whenever one class properties are inherited into number of classes is known as “Hierarchical Inheritance”. 
Java Images
Hierarchical Inheritance program
class A 
{ 
void display() { 
System.out.println("Method of super class A"); 
} 
} 
class B extends A 
{ 
void show() { 
System.out.println("Method of sub class B\n"); 
} 
} 
class C extends A 
{ 
void show() { 
System.out.println("Method of sub class C"); 
} 
} 
public class HierarchicalInheritance 
{ 
public static void main(String args[]) { 
B b = new B(); 
b.display(); 
b.show(); 
C c = new C(); 
c.display(); 
c.show(); 
} 
}
Output :
Method of super class A,, Method of sub class B,,Method of sub class C
Hybrid inheritance
The combination of multi-level and hierarchical inheritance is known as “Hybrid Inheritance”. 
Java Images
Multiple inheritance
The process of getting properties and behaviors form more than one super class to the one child class. The multiple inheritance is not possible in the java language so one class can extends only one class at time it is not possible to extends more than one class at time.

 We know that in multiple inheritance, sub classes are derived from multiple super classes. If two super classes have same names for their members (variables & methods) then which super class member is inherited into the subclass is the main confusion in multiple inheritance. This confusion is called “ambiguity problem”. So, Java does not support multiple inheritance at class level because of ambiguity problem.
Java Images