Last Updated on August 25, 2023 by Mayank Dham
In the realm of object-oriented programming, the concept of inheritance in Java stands as a cornerstone, empowering developers to build robust, extensible, and organized codebases. Java, renowned for its object-oriented principles, offers a sophisticated inheritance mechanism that allows classes to inherit properties and behaviors from their parent classes. This mechanism not only fosters code reusability but also promotes a structured approach to software design.
Whether you’re a novice programmer seeking to grasp the fundamentals of object-oriented programming or an experienced developer looking to refine your understanding of inheritance, this article aims to serve as your comprehensive guide. In this article, we are going to study what exactly inheritance is in java and the types of inheritance in java that include single level inheritance in Java, multi level inheritance in Java, Hierarchical inheritance in Java, hybrid inheritance in Java. Along with that, we will study each case’s inheritance program in java to clearly understand its working and output.
What is Inheritance in Java?
Inheritance is one of the many features of Oriented Programming. With the help of inheritance, an object belonging to a class can inherit the functions and properties of another class. With the help of inheritance, the reusability of code is increased and code repetition can be avoided.
Inheritance in Java can be performed when we create a class, the extend keyword integrates all the properties and functions existing in the included class.
Terminologies to Know Before Learning Inheritance in Java
Class:
Class can be abstractly unit having particular properties and features and defined to create objects possessing the same properties and features.
Parent Class:
The Class whose properties and features are included in the newly created class or child class is known as the parent class. Parent Class must be defined already to be included in the child class. It can also be called a base class.
Child Class:
The class which gets the features such as properties and features from the parent class or base class is known as the child class. It is created with an extended keyword acquiring all the properties and features from the parent class. It is also known as a subclass, extended class or derived class.
Types of Inheritance in Java
Now that we have a clear picture of inheritance in our that how it works and what inheritance in java is based on, we will move on to study the types of inheritance in java and also various inheritance programs in java for each of the cases.
The fundamental idea of inheritance is the creation of a class with features of an already pre-existing class to increase reusability and avoid any form of code repetition or redundancy.
The four types of inheritance that exist in java are as follows:-
- Single Level Inheritance
- Multi-Level Inheritance
- Hybrid Inheritance
- Hierarchical Inheritance
Single Level Inheritance
This is the most simple form of inheritance in java. In Single Level Inheritance, the child class inherits the properties and features of the parent class directly.
This scenario can be justified as the direct transmission of properties and behaviour from the parent class to the child class. The single-level inheritance program in java will help understand the working with more clarity.
class Pets { void eat() { System.out.println("Eat\n"); } } class Dog extends Pets { void bark() { System.out.println("Bark\n"); } } class Check{ public static void main(String args[]){ Dog d=new Dog(); d.bark(); d.eat(); } }
Output:
Bark
Eat
As it can be understood by the above Single Level Inheritance program in java, the Class Pet is the parent class while the class Dog is the child class as this real-world example shows how parent class and child class mechanisms work.
Multilevel inheritance in java
One of the types of inheritance in java where there are multiple classes involved in the transmission of properties and features. As the name suggests, there are many levels or perhaps multiple parties that are classes involved in sharing the class with child classes.
A class can derive properties from a parent class that has already derived properties from a different parent class thus, indicating multilevel inheritance from one class to another. Let’s look at the multilevel inheritance program in java for better understanding
of class Pets{ void eat(){ System.out.println("eat"); } } class Dog extends Pets { void bark(){ System.out.println("bark"); } } class Puppy extends Dog{ void weep(){ System.out.println("weep\n"); } } class Check{ public static void main(String args[]) { Puppy d=new Puppy(); d.weep(); d.bark(); d.eat(); } }
Output:
Weep
Bark
Eat
In the multilevel inheritance code in java, Pet can be assumed as the grandparent class that donated its properties to Dog, let’s assume that as our parent class and finally the properties that Dog had access to were donated to the child class in this context of multilevel inheritance.
Hierarchical Inheritance in Java
If more than one child class acquires the properties from the same parent class, it is said to be an instance of hierarchical inheritance. Let us understand this using the Hierarchical Inheritance program in Java
class Pets { void eat() { System.out.println("eat"); } } class Dog extends Pets{ void bark(){ System.out.println("bark\n"); } } class Cat extends Pets{ void meow(){ System.out.println("meow\n"); } } class Check{ public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); } }
Output:
Meow
Eat
As the above Hierarchical based inheritance program in Java states that subclasses Cat and Dog were able to inherit the properties from the parent class, Pets.
Hybrid Inheritance in Java
Among the various types of Inheritance in Java can be a combination of multiple types of inheritance in Java as discussed in the above section.
Now we are done with all the types of inheritance in java, looking at working on each of the cases of inheritance.
Conclusion
Inheritance in Java is not merely a concept of code reuse; it’s a dynamic mechanism that enables developers to create hierarchies of classes that reflect real-world relationships while promoting efficient and organized code. Through this exploration of inheritance, we’ve journeyed from understanding the basic principles to delving into the intricacies of method overriding, access modifiers, and the various types of inheritance Java offers.
By mastering inheritance, you’ve gained a powerful tool to craft flexible, modular, and extensible software systems. You’re now equipped to create base classes with shared attributes and behaviors, and derive specialized classes that enhance or override those features to suit specific needs. Moreover, as you venture deeper into the world of Java and object-oriented programming, your understanding of inheritance will form a solid foundation for exploring more advanced concepts and design patterns.
FAQs related to Inheritance in Java
Here are some FAQs related to types of inheritance in Java.
1. Is multiple inheritance supported in Java?
Java doesn’t support true multiple inheritance (inheritance from multiple classes). However, it offers a form of multiple inheritance through interfaces, where a class can implement multiple interfaces to gain different sets of behavior.
2. What are the types of inheritance in Java?
Java supports multiple types of inheritance:
Single Inheritance: A class can inherit from only one superclass.
Multiple Inheritance (through interfaces): A class can implement multiple interfaces, each providing a set of method declarations.
Multilevel Inheritance: A class inherits from another class, which itself inherits from a third class.
Hierarchical Inheritance: Multiple classes inherit from a single superclass.
Hybrid Inheritance: A combination of two or more types of inheritance.
3. How is method overriding related to inheritance?
Method overriding allows a subclass to provide a specific implementation for a method that is already defined in its superclass. This enables customization of behavior in subclasses while maintaining a consistent interface.
4. What are access modifiers and their role in inheritance?
Access modifiers (such as public, private, protected, and default) control the visibility and accessibility of class members. Inheritance is influenced by access modifiers, determining which members are accessible in subclasses and in other packages.
5. Can we inherit constructors in Java?
Constructors are not inherited in Java. However, a subclass can call a constructor from its superclass using the super() keyword to initialize inherited members.