Inheritance in Java programming language
In this tutorial, we will discuss the subject of Inheritance in Java programming language
Inheritance is one of the most effective concepts or feature of Oop paradigm. This helps to establish a link or connectivity between two or more classes. Inheritance helps to access properties(State and behaviours) and methods of one or more classes from another class. In other words, the derived class (subclass) inherit the states and behaviours from the base class(superclass). Java uses ‘extends’ keyword to establish this relationship.
A Base (super) class can have any number of derived(sub) class but a derived class can only have one base class. It’s because Java does not support multiple inheritances.
A superclass (parent class) can have any number of subclasses(child class). However, subclasses can only have one superclass.
“extends” is the keyword used to inherit the properties of a class.
The derived class inherits all the data members and methods that are declared as public or protected. If it’s declared as private, it cannot be inherited by the derived class.
The private members can be accessed only from their own class.
Types of inheritance
- Single inheritance
- Multiple inheritance
- Hierarchical inheritance
- Multilevel inheritance
Single inheritance is easy to understand as a class extends only to one another class. It is known as single inheritance. Single inheritance is having only one parent and child class. Child class has its own properties and those authorised by the parent class.
Public class A{ //parent class
Block of code…
}public class B extends A{ //child class
Block of code…
}
Multi-level inheritance
Multilevel inheritance means one base class extends to two or more (more than one) derived classes. A class can be the child(derived) class as well as a parent class to other classes. As seen in the diagram below, Class B inherits the properties of class A and Class C inherits the properties of class B. Class A is the parent to Class B and Class B is the parent to Class C.
Multilevel inheritance in java with Example
Public class A{
Block of code…
}
public class B extends A{
Block of code…
}
public class C extends B{
Block of code…
}
……may continue…
..
In multiple inheritance, one class extends to more than one base class(one child and two or more parent class). Multiple inheritance
is not supported in java. Java supports
interface instead of multiple inheritance.
Public class A{
Block of code…
}
public class B {
Block of code…
}
public class C extends A,B{
Block of code…
}
Java not support this inheritance
In the hierarchical inheritance, one parent class will be inherited by many children classes. In the example below, class A is inherited by Class B, Class C and class D. Class A will be acting as the base class for class B and class C and D.
Hierarchical inheritance with example in java
Public class A{
Block of code…
}
public class B extends A{
Block of code…
}
public class C extends A{
Block of code…
}
Related Links