utside the realm of computer science, the term “inheritance” generally refers to the biological phenomenon of children acquiring the traits and characteristics of their parents. But interestingly, inheritance in object-oriented programming languages (OOPs) like Java points towards the similar concept of “acquiring” something. In OOPs, inheritance essentially refers to one class acquiring the properties of another class. The class with inherited properties is the parent class, and the class that inherits the properties is known as the child class.
This article will walk you through the fundamentals of inheritance in Java and the different types of inheritance in Java with examples.
What is inheritance in Java?
Inheritance in Java is how one class acquires the functionalities (methods) and properties (data members) of another class. The idea behind inheritance in Java is to create new classes built upon existing classes. The class whose functionalities and properties are used or inherited is the parent class, base class, or superclass. On the other hand, the class that extends the features of the parent class is the child class, derived class, or the subclass.
Therefore, inheritance in Java imparts the feature of code reusability. When we inherit from an existing class, we can extend the fields and methods of the parent class while adding new methods and fields in our current class.
Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
The Syntax for Inheritance in Java
We use the extends keyword to inherit a class in Java. For example,
class XYZ extends ABC
{
}
Here, class ABC is the parent class, and XYZ is the child class. Class XYZ is inheriting the methods and properties of class ABC.
Example of Inheritance in Java
Let’s understand the practical implication of Java inheritance through the example code below:
class Sportsperson {
String designation = “Sportsperson”;
String eventName = “Olympics”;
void does(){
System.out.println(“Sports”);
}
}
public class Athlete extends Sportsperson{
String mainSubject = “Javelin throw”;
public static void main(String args[]){
Athlete obj = new Athlete();
System.out.println(obj.eventName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}
}
Output:
Olympics
Sportsperson
Javelin throw
Sports
In the above example, we have a parent class Sportsperson and a child class Athlete. Since class Athlete extends the designation and event properties and does()method from the parent class, we need not declare these methods and properties in the child class. However, eventName, designation, and does()method are common to all sportspersons, and hence, we have declared them in the parent class. In this way, the child classes like Athlete, Gymnast, Archer, etc., can use the methods and properties from the parent class instead of writing the common code every time.
Types of inheritance in Java
Java has five types of inheritance.
Let’s understand each type with an example.
1. Single Inheritance in Java
Single inheritance in Java refers to a parent and child class relationship where one class extends another one class only. In the above diagram, class B only extends ClassA. Class A is the parent class, and Class B is the child class.
Now, let’s look at a simple code to understand single inheritance in Java:
class Flower{
void color(){System.out.println(“Red”);}
}
class Rose extends Flower{
void smell(){System.out.println(“Fragrant”);}
}
class TestInheritance{
public static void main(String args[]){
Rose r=new Rose();
r.smell();
r.color();
}
}
Output:
Red
Fragrant
In the above example, the Rose class inherits the Flower class, so there is single inheritance.
2. Multilevel Inheritance in Java
In multilevel inheritance, one class extends from a child class. Therefore, the child class becomes the parent class for the new class. For example, in the above diagram, Class C is the child class of B, and Class B is a child class of A.
Let’s see an example in code:
class Animal{
void eat(){System.out.println(“Wild”);}
}
class Tiger extends Animal{
void sound(){System.out.println(“Roar”);}
}
class Cub extends Tiger{
void food(){System.out.println(“Milk”);}
}
class TestInheritance2{
public static void main(String args[]){
Cub c=new Cub();
c.food();
c.sound();
c.eat();
}
}
Output:
Milk
Roar
Wild
In the above example, class Cub inherits the Tiger class, which inherits the Animal class. Thus, there is inheritance at multiple levels.
3. Multiple Inheritance in Java
Multiple inheritance in Java is the mechanism of one class extending more than one class. Therefore, a child class has more than one parent class. Class C (child class) extends both Classes A and B (parent class) in the above diagram.
However, Java does not support multiple inheritance because a problem occurs when methods with the same signature exist in both the parent and the child classes. Thus, when the method is called, the compiler fails to identify which class method should be called or which class method should be prioritized. The result is a compile-time error.
Let’s see an example to understand the glitch.
class X{
void msg(){System.out.println(“Hello”);}
}
class Y{
void msg(){System.out.println(“Good Morning!”);}
}
class Z extends X,Y{ //suppose if it were
public static void main(String args[]){
Z obj=new Z();
obj.msg(); //Now which msg() method would be invoked?
}
}
Consider an example where X, Y, and Z are three classes, and the Z class inherits the X and Y classes. If X and Y classes have the same method and we call it from child class object, there is an ambiguity regarding whether to call the method of Class X or Class Y. Therefore, there is a compile-time error.
4. Hierarchical Inheritance in Java
In hierarchical inheritance, many child classes inherit from one parent class. As evident in the above diagram, Class A is the parent class that inherits the chid classes B, C, and D.
Here’s an example of hierarchical inheritance in Java:
class Forest{
void components(){System.out.println(“Trees”);}
}
class Coniferous extends Forest{
void location(){System.out.println(“Mountains”);}
}
class Tropical extends Forest{
void where(){System.out.println(“Plains”);}
}
class TestInheritance3{
public static void main(String args[]){
tropical t=new Tropical();
t.where();
t.components();
//c.bark();//Compile-time Error
}
}
Output:
Plains
Trees
In the above example, the Coniferous and Tropical classes inherit the Forest class, showing hierarchical inheritance.
5. Hybrid Inheritance in Java
Hybrid inheritance combines more than one type of inheritance in a single program. For instance, in the above diagram, classes B and C inherit from Class A. Again, Class D inherits from both classes B and C. Therefore, it is a combination of hierarchical and multiple inheritance.
Since Java does not support multiple inheritance with classes, it is impossible to implement hybrid inheritance with classes. In Java, hybrid inheritance is only possible through interfaces.
With that, we come to the end of our discussion on inheritance in Java and its types.
In Conclusion
Inheritance in Java is one of the most fundamental OOP concepts and refers to the mechanism of a class inheriting the properties of another class. Based on the mode of inheritance, there are five types, out of which Java does not support multiple inheritance and hybrid inheritance. The other three types are single inheritance, multilevel inheritance, and hierarchical inheritance. The keyword “extends” is crucial in Java inheritance syntax.
If you want to learn more Java concepts in-depth and learn in-demand software development skills, upGrad offers a Master of Science in Computer Science program from Liverpool John Moores University.
Program Highlights:
- Specially designed for working professionals
- Master’s Degree from LJMU and Executive PGP from IIIT Bangalore
- Certification in Data Science & Machine Learning
- 500+ hours of learning with 30+ case studies and projects
- 30+ tools and software, including Java
- 360-degree learning support
- Industry networking and peer learning
Join the 40,000+ learner base of upGrad and experience the cutting-edge learning experience from a platform that has impacted more than 500,000 working professionals worldwide!
What is inheritance in Java with real-time example?
Inheritance in Java is the ability of one class to inherit the properties of another class. For instance, a car inherits the properties of automobiles, which in turn inherits some of the properties of vehicles.
How multiple inheritance is used in Java?
The only way to implement multiple inheritance in Java is to use interfaces in a class. A class can implement multiple interfaces and solves the problem of multiple inheritance. The extends keyword is used only once, and the parent interfaces are declared in the form of a comma-separated list. For example, if the interface Cricket extended both Event and Sports, it would be expressed as: public interface Cricket extends Event, Sports
How many types of inheritance are there in OOPs?
Object-oriented programming languages (OOPs) have five types of inheritance: single inheritance, multilevel inheritance, multiple inheritance, hierarchical inheritance, and hybrid inheritance. Out of these, Java does not support multiple inheritance and hybrid inheritance.