Inheritance in Java is an object-oriented concept where one class (child/subclass) inherits the properties and methods of another class (parent/superclass). This powerful feature helps with code reuse, method overriding, and polymorphism. At Obsqura Zone, a leading software testing training institute in Kerala, we emphasize understanding these concepts to build efficient and maintainable automation frameworks.
This blog will walk you through
- Importance of inheritance in java
- Definition of inheritance
- Terms used in inheritance
- Types of inheritance and examples
Definition of inheritance
Inheritance is an Object-Oriented Programming (OOPS) concept in which one class (child/subclass) acquires the properties and behaviors (variables and methods) of another class (parent/superclass).
It allows creating new classes based on existing ones, promoting code reusability, extensibility, and maintainability.
In the light of OOPS, inheritance holds an IS-A relationship. For instance, Dog inherits the properties of Animal class. Hence, Dog is an Animal.
Terms used in OOPS inheritance are as below:
- Parent Class (Superclass / Base Class) : Class that attributes and data are being inherited.
- Child Class (Subclass / Derived Class) : Class that base class attributes and data are inherited into.
- Extends : Java uses the extends keyword for inheritance.
- Inherited Members : Variables and methods that are inherited from the parent class.
- Method Overriding : When a child class provides its own implementation of a method already defined in the parent class
- super keyword : super keyword is used to refer to the parent class object.
- Code Reusability : Code reusability refers to the ability to reuse existing code through inheritance.
- Access modifiers :Access modifiers determine the visibility of inherited members:
- public
- protected
- default
- private (not inherited)
- Final : Final keyword is used to denote the classes and methods that cannot be inherited nor overridden
Types of inheritance and examples
Java supports five types of inheritance. But, multiple inheritance is applied using interfaces instead of classes.
1. Single Inheritance
One child class inherits from one parent class.
| class A { void show() { System.out.println(“Class A”); } } class B extends A { void display() { System.out.println(“Class B”); } } |
2. Multilevel Inheritance
A class is derived from another derived class.
| class A { } class B extends A { } class C extends B { } |
3. Hierarchical Inheritance
Multiple child classes inherit from a single parent class.
| class A { } class B extends A { } class C extends A { } |
4. Multiple Inheritance (Using Interfaces)
A class inherits from multiple interfaces.
| interface A { void methodA(); } interface B { void methodB(); } class C implements A, B { public void methodA() { } public void methodB() { } } |
5. Hybrid Inheritance
Combination of two or more types of inheritance. Below is an example integrating Single level inheritance using interface and multiple inheritance.
| interface A { } interface B extends A { } class C implements B { } |
4. Multiple Inheritance (Using Interfaces)
A class inherits from multiple interfaces.
| interface A { void methodA(); } interface B { void methodB(); } class C implements A, B { public void methodA() { } public void methodB() { } } |
5. Hybrid Inheritance
Combination of two or more types of inheritance. Below is an example integrating Single level inheritance using interface and multiple inheritance.
| interface A { } interface B extends A { } class C implements B { } |
Importance of inheritance in java
Inheritance is a key concept of Object-Oriented Programming (OOPS) that allows one class to acquire the properties and behaviors of another class. Its importance lies in how it makes programs efficient, organized, and reusable.
1. Code Reusability
Inheritance allows reuse of existing code without rewriting it.
📌 Example:
A Dog class can reuse methods of the Animal class like eat() and sleep().
2. Reduces Code Redundancy
Common code is written once in the parent class and shared by all child classes, reducing duplication.
3. Supports Method Overriding
Child classes can provide their own implementation of parent methods, enabling runtime polymorphism.
4. Improves Maintainability
Changes made in the parent class automatically apply to child classes, making maintenance easier.
5. Enables Polymorphism
Inheritance allows a parent class reference to refer to a child class object.
6. Establishes IS-A Relationship
Inheritance clearly represents real-world relationships between objects.
7 . Better Program Structure
Helps organize code into hierarchical levels, improving readability and design.
8. Enhances Extensibility
New features can be added by creating new child classes without modifying existing code.
Combination of two or more types of inheritance. Below is an example integrating Single level inheritance using interface and multiple inheritance.
Inheritance is a fundamental concept of Object-Oriented Programming in Java that enables one class to acquire the properties and behaviors of another class. It promotes code reusability, reduces redundancy, and helps organize programs in a clear hierarchical structure. By supporting method overriding and polymorphism, inheritance makes Java programs more flexible, extensible, and easier to maintain. At Obsqura Zone, a leading software testing training institute in Kerala, we teach how inheritance plays a crucial role in building efficient, scalable, and well-structured Java applications, helping learners master core Java concepts for real-world automation testing projects.