Curriculum
Inheritance is one of the most powerful features of Object-Oriented Programming (OOP) in Java. It allows one class to acquire the properties and behaviors of another class, promoting code reusability, scalability, and maintainability. Instead of rewriting common functionality multiple times, developers can create a parent class and allow child classes to inherit its features.
In modern software development, inheritance is extensively used in enterprise applications, Spring Boot projects, banking systems, e-commerce platforms, healthcare software, and backend services. Understanding Inheritance is essential because it helps developers build organized and reusable software architectures.
In this lesson, you will learn what inheritance is, why it is important, the different types of inheritance, how inheritance works in Java, and real-world examples of inheritance in backend development.
Inheritance is a mechanism through which one class acquires the properties and methods of another class.
The class being inherited is called the:
Parent Class
or
Superclass
The class that inherits is called the:
Child Class
or
Subclass
Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
}
Here:
The Dog class automatically inherits the eat() method.
Inheritance provides several advantages.
Common functionality can be written once and reused.
Developers do not need to rewrite existing code.
Changes in the parent class automatically affect child classes.
Applications become more structured and easier to manage.
Inheritance is a core concept of object-oriented design.
These benefits make inheritance an essential programming technique.
Consider the example of vehicles.
A Vehicle may have:
Cars, bikes, and trucks share these common characteristics.
Instead of duplicating code, a parent Vehicle class can be created.
Example:
class Vehicle {
String brand;
void start() {
System.out.println("Vehicle Started");
}
}
Child class:
class Car extends Vehicle {
}
The Car class automatically inherits all properties and methods from Vehicle.
Java uses the extends keyword.
Example:
class Parent {
}
class Child extends Parent {
}
The extends keyword establishes the inheritance relationship.
Parent class:
class Animal {
void eat() {
System.out.println("Animal Eats Food");
}
}
Child class:
class Dog extends Animal {
}
Object creation:
Dog dog = new Dog();
dog.eat();
Output:
Animal Eats Food
Even though eat() belongs to Animal, Dog can access it through inheritance.
Inheritance applies to variables as well.
Parent class:
class Employee {
String company = "Forsk Software";
}
Child class:
class Developer extends Employee {
}
Usage:
Developer dev = new Developer();
System.out.println(dev.company);
Output:
Forsk Software
The child class inherits both methods and variables.
Example:
class Person {
String name;
}
class Student extends Person {
int rollNumber;
}
Object:
Student student = new Student();
student.name = "Rahul";
student.rollNumber = 101;
Output:
System.out.println(student.name);
System.out.println(student.rollNumber);
Result:
Rahul
101
The child class contains both inherited and own properties.
Java supports several forms of inheritance.
One child inherits from one parent.
Example:
class Animal {
}
class Dog extends Animal {
}
This is the most common type.
Inheritance chain involving multiple levels.
Example:
class Animal {
}
class Mammal extends Animal {
}
class Dog extends Mammal {
}
Dog inherits from Mammal, which inherits from Animal.
This creates a hierarchy.
Multiple child classes inherit from one parent.
Example:
class Animal {
}
class Dog extends Animal {
}
class Cat extends Animal {
}
class Lion extends Animal {
}
All child classes share the same parent.
Some languages allow:
class C extends A, B
Java does not support this.
Reason:
It can create ambiguity problems.
Example:
If both parent classes contain the same method:
show()
Java would not know which implementation to use.
This issue is commonly called the:
Diamond Problem
To avoid ambiguity, Java does not allow multiple inheritance through classes.
Java provides the super keyword to access parent class members.
Example:
class Animal {
String type = "Animal";
}
class Dog extends Animal {
void display() {
System.out.println(super.type);
}
}
Output:
Animal
super refers to the parent class.
Example:
class Animal {
void sound() {
System.out.println("Animal Sound");
}
}
class Dog extends Animal {
void display() {
super.sound();
}
}
Output:
Animal Sound
The child class explicitly calls the parent method.
When a child object is created, the parent constructor executes first.
Example:
class Animal {
Animal() {
System.out.println("Animal Constructor");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog Constructor");
}
}
Object:
Dog dog = new Dog();
Output:
Animal Constructor
Dog Constructor
Java automatically invokes the parent constructor.
A child class can redefine a method inherited from its parent.
Example:
class Animal {
void sound() {
System.out.println("Animal Sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog Barks");
}
}
Object:
Dog dog = new Dog();
dog.sound();
Output:
Dog Barks
This concept is called method overriding.
Method overriding is closely related to polymorphism.
Parent class:
class Account {
void accountType() {
System.out.println("General Account");
}
}
Child class:
class SavingsAccount extends Account {
}
Object:
SavingsAccount account = new SavingsAccount();
account.accountType();
Output:
General Account
The child class inherits banking functionality.
Parent class:
class Employee {
void work() {
System.out.println("Employee Working");
}
}
Child class:
class Manager extends Employee {
}
Usage:
Manager manager = new Manager();
manager.work();
Output:
Employee Working
Inheritance reduces duplication across employee types.
Write code once and use it multiple times.
Applications become easier to expand.
Common functionality remains centralized.
Updates can be performed in one place.
Software architecture becomes clearer.
These advantages make inheritance a cornerstone of OOP.
Inheritance should be used carefully.
Potential disadvantages include:
Modern software design often combines inheritance with composition for better maintainability.
Not every class should inherit from another class.
Use inheritance only when an “is-a” relationship exists.
Example:
Dog is an Animal
Valid inheritance.
Example:
Engine is a Car
Invalid inheritance.
An engine belongs to a car but is not a car.
Avoid excessively long inheritance hierarchies.
They make applications difficult to understand and maintain.
Ensure method signatures match correctly when overriding.
These practices help build maintainable software systems.
Inheritance is heavily used in backend applications.
Examples include:
User
├── Admin
├── Customer
└── Employee
Payment
├── CreditCardPayment
├── UPI
└── NetBanking
Notification
├── EmailNotification
├── SMSNotification
└── PushNotification
Framework classes frequently use inheritance to provide reusable functionality.
Inheritance helps developers create scalable enterprise applications.
Inheritance is a powerful Object-Oriented Programming concept that allows one class to acquire the properties and methods of another class. It promotes code reusability, maintainability, scalability, and better software organization.
Java supports:
Inheritance enables developers to model real-world relationships effectively while reducing code duplication. It is widely used in enterprise software, backend systems, Spring Boot applications, and object-oriented design.
Mastering inheritance is essential before learning the next OOP concept, polymorphism.
Inheritance is a mechanism through which one class acquires the properties and methods of another class.
Java uses the extends keyword to implement inheritance.
No. Java does not support multiple inheritance through classes to avoid ambiguity issues.
The super keyword is used to access parent class variables, methods, and constructors.
Inheritance improves code reusability, scalability, maintainability, and software organization.
Want to explore additional programming and software development topics? Click here for more free courses
WhatsApp us