Java - Object-Oriented Programming (OOPS) Concept - Inheritance Tutorial
Inheritance is a process of copying all property and behavior from parent class to child class.
- It provides code reusability.( Reusability means child class can use methods and fields of parents class )
- It can also be used to achieve runtime polymorphism using method overriding.
Child class (Subclass) can use the methods and field of parents class(super Class) after inheritance.
For inheritance between parents class and child class, use the extends
keyword.
extends
keyword is used to gain the functionality into child class using parents class.
Syntax for inheritance
class ParentClass {
.....
}
class ChildClass extends ParentClass {
.....
}
Example 1-
class Bird{ //Parent Class
String name2="Bird";
void fly(){
System.out.println(" can Fly");
}
}
public class Parrot extends Bird{ //Child Class
String name1="Parrot";
void color(){
System.out.print(" is a green color Bird. ");
}
public static void main(String args[]){
Parrot p=new Parrot();
System.out.print(p.name1);
p.color();
System.out.print(p.name2);
p.fly();
}
}
Example 1 Explanation-
As in the above example, Parrot is SubClass i.e ChildClass and Bird is SuperClass i.e ParentClass. The relationship between child class and parent class using extends keyword to show that Parrot is a type of Bird Class.
Here Parrot Class can use the field and method of own class as well as Bird Class.
Types Of Inheritance in Java
- Single
- Multilevel
- Hierarchical
Some of the inheritance can be used through interface only that are
- Multiple
- Hybrid
This two inheritance cannot be used normally like other inheritance
- Single Inheritance
When single Child Class inherits the property of single Parent class. This type of inheritance is known as single inheritance.
class ParentClass {
.....
}
class ChildClass extends ParentClass {
.....
}
Eg-
class Bird{ //Parent Class
String name2="Bird";
}
public class Parrot extends Bird{ //Child Class
String name1="Parrot";
public static void main(String args[]){
Parrot p=new Parrot();
System.out.println(p.name1+" is "+p.name2);
}
}
- Multilevel Inheritance
In multilevel inheritance, there can be any number of class that forms a chain-like structure.
In the above example, there is childclass1 which inherits the property of childclass2. Childclass2 which inherits the property of Childclass3. Childclass3 which inherits the property of ParentClass.
And therefore in this example, childclass1 can access the method and field of ParentClass.
class ParentClass {
.....
}
class ChildClass3 extends ParentClass {
.....
}
class ChildClass2 extends ChildClass3 {
.....
}
class ChildClass1 extends ChildClass2 {
.....
}
Example-
class Species{ //Parent Class
String name3="Species";
}
class Bird extends Species{ //Parent Class
String name2="Bird";
}
public class Parrot extends Bird{ //Child Class
String name1="Parrot";
public static void main(String args[]){
Parrot p=new Parrot();
System.out.println(p.name1+" is "+p.name2+". "+p.name2+" is "+p.name3);
}
}
In the above example, the Species variable name3 can be accessed using the Parrot object.
- Hierarchical Inheritance
When two or more Child classes can inherit a single parent class. It is known as Hierarchical inheritance.
In the above example there is childclass1 and childclass2 which inherit the property of ParentClass. Here one Childclass cannot use the property i.e is method and field of other Child classes. But both the child class can use the property of the Parent class
class ParentClass {
.....
}
class ChildClass2 extends ParentClass {
.....
}
class ChildClass1 extends ParentClass {
.....
}
Example1
class Bird{ //Parent Class
String name3="Bird";
}
class Crow extends Bird{ //Child Class 2
String name2="Crow";
}
class Parrot extends Bird{ //Child Class1
String name1="Parrot"; }
public class HierarchicalInheritance{
public static void main(String args[]){
Parrot p=new Parrot();
System.out.println(p.name1+" is "+p.name3);
//System.out.println(p.name2+" is "+p.name3); this line will give error: cannot find symbol name2
}
}
- Multiple Inheritance
In this inheritance, one child class can inherit multiple parents class.
This inheritance is only supported using an interface to prevent ambiguity.
class Bird1{ //Parent Class
String name3="Bird1";
}
class Bird2{ //Child Class 2
String name2="Bird2";
}
class Parrot extends Bird1,Bird2{ //Child Class1
String name1="Parrot"; }
public class MultipleInheritance{
public static void main(String args[]){
Parrot p=new Parrot();
System.out.println(p.name1+" is "+p.name3);
}
}
The above program will give an error. Therefore to prevent such error multiple inheritances are only used with the interface.
- Hybrid Inheritance
Hybrid inheritance is a combination of hierarchical and multiple.
As it also creates ambiguity. Hence this inheritance is not used in java.