Inheritance in Java
Learn how inheritance allows one class to acquire accessible fields and methods from another class and how Java supports different inheritance structures.
What is Inheritance?
Inheritance is an object-oriented programming mechanism in which a class derives from another class. The derived class can reuse accessible members of the parent class and can add its own state and behavior.
In Java, inheritance between classes is created using the
extends keyword.
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
Parent and Child Classes
| Term | Meaning |
|---|---|
| Superclass | The class whose members are inherited. |
| Subclass | The class that extends the superclass. |
| Parent Class | Another common name for superclass. |
| Child Class | Another common name for subclass. |
Basic Inheritance Example
class Vehicle {
void start() {
System.out.println("Vehicle started");
}
}
class Car extends Vehicle {
void drive() {
System.out.println("Car is driving");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
car.drive();
}
}
Vehicle started
Car is driving
Code Reuse
Inheritance can reduce duplication when a group of classes shares common behavior.
Vehicle
│
├── start()
├── stop()
│
↓
Car
│
├── drive()
└── honk()
1. Single Inheritance
Single inheritance occurs when one subclass extends one superclass.
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
Animal
↓
Dog
2. Multilevel Inheritance
Multilevel inheritance occurs when a class extends another class that itself extends another class.
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
class Puppy extends Dog {
void play() {
System.out.println("Playing");
}
}
Animal
↓
Dog
↓
Puppy
3. Hierarchical Inheritance
Hierarchical inheritance occurs when multiple subclasses extend the same superclass.
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Meowing");
}
}
Animal
/ \
↓ ↓
Dog Cat
4. Multiple Inheritance Through Interfaces
Java does not allow a class to extend multiple classes. However, a class can implement multiple interfaces.
interface Camera {
void takePhoto();
}
interface GPS {
void navigate();
}
class SmartPhone implements Camera, GPS {
public void takePhoto() {
System.out.println("Taking photo");
}
public void navigate() {
System.out.println("Navigating");
}
}
A class can implement multiple interfaces, allowing it to combine contracts from multiple sources.
5. Hybrid Inheritance Through Interfaces
Hybrid inheritance combines more than one inheritance structure. Java avoids multiple inheritance of classes but can express more complex designs through interfaces.
class Animal {
void eat() {
System.out.println("Eating");
}
}
interface Swimmable {
void swim();
}
interface RunnableAnimal {
void run();
}
class Duck extends Animal
implements Swimmable, RunnableAnimal {
public void swim() {
System.out.println("Swimming");
}
public void run() {
System.out.println("Running");
}
}
IS-A Relationship
Inheritance represents an IS-A relationship.
Dog IS-A Animal
Car IS-A Vehicle
Manager IS-A Employee
The extends Keyword
The extends keyword establishes class
inheritance.
class CiitStudent {
void attendClass() {
System.out.println("Attending regular lectures at CIIT Institute 🤓👨🏫...!");
}
}
class CiitGraduate extends CiitStudent {
void attendPlacement() {
System.out.println("Attending the CIIT Campus Placement Drive for top tech jobs 👀🤓...!");
}
}
The super Keyword
The super keyword refers to the immediate
superclass. It can be used to access accessible superclass
members and invoke superclass constructors.
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog";
void display() {
System.out.println(name);
System.out.println(super.name);
}
}
Dog
Animal
Calling a Superclass Method
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
super.sound();
System.out.println("Dog bark");
}
}
Calling a Superclass Constructor
class Person {
String name;
Person(String name) {
this.name = name;
}
}
class Student extends Person {
int age;
Student(String name, int age) {
super(name);
this.age = age;
}
}
super(name) invokes the constructor of the
immediate superclass.
Inherited Methods
A subclass can use accessible methods inherited from its superclass.
class Vehicle {
void start() {
System.out.println("Starting");
}
}
class Car extends Vehicle {
}
Car car = new Car();
car.start();
Method Overriding
A subclass can provide a specific implementation of an inherited instance method. This is called method overriding.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
The @Override annotation tells the compiler that
the method is intended to override an inherited method.
final and Inheritance
A class declared with final cannot be extended.
final class SecurityManager {
}
A method declared final cannot be overridden by
a subclass.
class Parent {
final void display() {
System.out.println("Parent");
}
}
Access Modifiers and Inheritance
| Modifier | Inherited/Accessible in Subclass |
|---|---|
public |
Yes, subject to normal access rules. |
protected |
Yes, with Java's protected-access rules. |
| No modifier | Accessible within the same package. |
private |
Not directly accessible from the subclass. |
Are Constructors Inherited?
No. Constructors are not inherited by subclasses.
However, a subclass constructor can invoke a superclass
constructor using super().
Object: The Root Class
Every Java class ultimately derives from
java.lang.Object, either directly or indirectly.
Object
↓
Person
↓
Employee
↓
Manager
This means classes inherit methods such as
toString(), equals(), and
hashCode() from Object, subject to
overriding and normal Java rules.
Inheritance vs Composition
Inheritance represents an IS-A relationship, while composition represents a HAS-A relationship.
| Inheritance | Composition |
|---|---|
| IS-A relationship. | HAS-A relationship. |
Uses extends. |
Uses object references. |
| Creates a class hierarchy. | Combines objects to build behavior. |
| Can create tighter coupling. | Often provides more flexible composition. |
Practical Example: Employee Hierarchy
class Employee {
protected String name;
Employee(String name) {
this.name = name;
}
void work() {
System.out.println(
name + " is working"
);
}
}
class Developer extends Employee {
Developer(String name) {
super(name);
}
void writeCode() {
System.out.println(
name + " is writing code"
);
}
}
public class Main {
public static void main(String[] args) {
Developer developer =
new Developer("Samadhan");
developer.work();
developer.writeCode();
}
}
Benefits of Inheritance
- Promotes reuse of common behavior.
- Helps model hierarchical relationships.
- Supports runtime polymorphism through overriding.
- Reduces duplication in suitable class hierarchies.
- Allows common behavior to be defined at a higher level.
Limitations of Inheritance
- Deep inheritance hierarchies can become difficult to understand.
- Subclasses can become tightly coupled to superclass implementation.
- Java does not support multiple inheritance of classes.
- Inheritance should be used only when the IS-A relationship is semantically appropriate.
Inheritance Best Practices
- Use inheritance for genuine IS-A relationships.
- Prefer composition when behavior is better modeled as a HAS-A relationship.
- Keep inheritance hierarchies reasonably shallow.
-
Use
@Overridewhen overriding methods. - Design superclass contracts carefully because subclasses depend on them.
Interview Questions
extends is used for class inheritance
and interface inheritance, while
implements is used when a class
implements one or more interfaces.
super().
super can refer to accessible
superclass members and can invoke superclass
constructors.
Summary
Inheritance allows a Java class to derive from another
class and reuse accessible behavior. Java supports
single, multilevel, and hierarchical class inheritance,
while multiple and more complex structures can be modeled
through interfaces. The super keyword,
method overriding, and careful use of composition are
important parts of inheritance design.