Core Java: Inheritance in Java
Inheritance is one of the important concepts of Object-Oriented Programming (OOP).
In simple words, inheritance means a child class can acquire the properties and behaviours of a parent class.
In Java, inheritance is achieved using the extends keyword.
For example, suppose we have a Vehicle class:
class Vehicle {
String brand = "Toyota"; void start() {
System.out.println("Vehicle is starting");
}
}Now we create a Car class that extends Vehicle:
class Car extends Vehicle { void drive() {
System.out.println("Car is driving");
}
}Now Car can use the properties and methods of Vehicle:
public class Main {
public static void main(String[] args) {Car car = new Car(); System.out.println(car.brand);
car.start();
car.drive();
}
}
Output:
Toyota
Vehicle is starting
Car is drivingHere:
Vehicleis the parent/superclassCaris the child/subclassextendsis used to create the inheritance relationship
So we can say:
A
Caris aVehicle.
This is also called an IS-A relationship.
Why do we need inheritance?
Inheritance mainly helps us with code reusability.
Imagine we have many different types of vehicles:
Vehicle
|
+---- Car
|
+---- Bike
|
+---- TruckInstead of writing start() separately inside every class, we can define it once in Vehicle and let the child classes inherit it.
This avoids duplicate code.
Types of Inheritance in Java
Java supports different forms of inheritance.
1. Single Inheritance
One child class inherits from one parent class.
Parent
|
ChildExample:
class Animal {
void eat() {
System.out.println("Eating");
}
}class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}Dog inherits from Animal.
2. Multilevel Inheritance
In multilevel inheritance, one class inherits from another class, and another class inherits from that child class.
Animal
|
Dog
|
PuppyExample:
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");
}
}
Now Puppy can access methods from both Dog and Animal.
Puppy puppy = new Puppy();puppy.eat();
puppy.bark();
puppy.play();So the inheritance happens across multiple levels.
3. Hierarchical Inheritance
Multiple child classes inherit from the same parent class.
Vehicle
/ \
Car BikeExample:
class Vehicle {
void start() {
System.out.println("Vehicle starts");
}
}class Car extends Vehicle {
void drive() {
System.out.println("Car drives");
}
}class Bike extends Vehicle {
void ride() {
System.out.println("Bike rides");
}
}
Both Car and Bike can use the start() method.
Does Java support Multiple Inheritance?
Java does not support multiple inheritance through classes.
That means we cannot do this:
class Child extends Parent1, Parent2 {
}Why?
One major reason is ambiguity.
Suppose Java allowed:
Parent1 Parent2
\ /
ChildAnd both parents had the same method:
class Parent1 {
void show() {
System.out.println("Parent 1");
}
}class Parent2 {
void show() {
System.out.println("Parent 2");
}
}If Child inherited from both, then:
Child obj = new Child();
obj.show();Which show() should Java execute?
Should it execute:
Parent 1or
Parent 2This creates ambiguity.
Therefore, Java does not allow multiple inheritance using classes.
However, Java does support multiple inheritance through interfaces, because a class can implement multiple interfaces and Java provides rules for resolving conflicts.
this Keyword
Now let’s understand the this keyword.
this refers to the current object.
It can be used for several purposes, such as:
- referring to the current object’s variables
- calling another method of the current object
- calling another constructor of the same class
For example:
class Student {String name;
int age; Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Here we have:
this.name = name;The this.name refers to the instance variable, while name refers to the constructor parameter.
this() Constructor Call
this() is used to call another constructor of the same class.
For example:
class Student {String name;
int age; Student() {
this("Unknown", 0);
} Student(String name, int age) {
this.name = name;
this.age = age;
}
}
When we create:
Student s = new Student();The no-argument constructor calls:
this("Unknown", 0);which calls the parameterized constructor of the same class.
One important rule:
this()must be the first statement inside a constructor.
super Keyword
super is used to refer to the parent class.
It can be used to:
- Call the parent class constructor
- Access the parent class variable
- Call the parent class method
For example:
class Animal {String name = "Animal"; void sound() {
System.out.println("Animal makes a sound");
}
}class Dog extends Animal { String name = "Dog"; void display() {
System.out.println(name);
System.out.println(super.name);
}
}
Here:
namerefers to the Dog variable.
Whereas:
super.namerefers to the Animal variable.
super() Constructor
super() is used to call the parent class constructor.
Get Aishu Reddy’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
class Animal {Animal() {
System.out.println("Animal constructor");
}
}class Dog extends Animal { Dog() {
super();
System.out.println("Dog constructor");
}
}
When we create:
Dog dog = new Dog();The output will be:
Animal constructor
Dog constructorThis happens because the parent constructor executes before the child constructor.
What happens if we don’t write super()?
This is an important point.
If we don’t explicitly write super() inside a child constructor, Java automatically inserts it as the first statement, provided the parent has an accessible no-argument constructor.
For example:
class Animal {Animal() {
System.out.println("Animal");
}
}class Dog extends Animal { Dog() {
System.out.println("Dog");
}
}
Java internally treats the constructor approximately like:
Dog() {
super();
System.out.println("Dog");
}So the parent constructor is called first.
The Object Class
There is another important concept here.
In Java, every class ultimately inherits from the Object class, either directly or indirectly.
For example:
Object
|
Animal
|
DogEven if we write:
class Animal {
}it is conceptually similar to:
class Animal extends Object {
}And if:
class Dog extends Animal {
}then:
Dog → Animal → ObjectSo when a Dog object is created, constructor execution ultimately reaches the Object constructor at the top of the inheritance hierarchy.
The constructor chain works from parent to child:
Object constructor
↓
Animal constructor
↓
Dog constructorThis is why we often say:
The first statement of a constructor is either an explicit
this()/super()call, or Java insertssuper()automatically.
If this() is used, that constructor calls another constructor in the same class, and eventually the chain must reach a superclass constructor.
Method Overriding
Now let’s talk about method overriding.
Method overriding happens when a child class provides its own implementation of a method that is already defined in the parent class.
Example:
class Animal {void sound() {
System.out.println("Animal makes a sound");
}
}class Dog extends Animal { @Override
void sound() {
System.out.println("Dog barks");
}
}
Both Animal and Dog have a method called:
sound()But Dog provides its own implementation.
Now:
Dog dog = new Dog();
dog.sound();Output:
Dog barksThe child implementation is executed.
The @Override annotation tells the compiler that we intend to override a parent method.
It is recommended because the compiler can catch mistakes for us.
For example, if we accidentally write:
@Override
void sounds() {
}Java will give a compilation error because sounds() does not actually override a method from the parent.
Why is Method Overriding Important?
Method overriding is one of the concepts that enables runtime polymorphism.
For example:
Animal animal = new Dog();animal.sound();Even though the reference type is Animal, the actual object is Dog.
Therefore, Java executes:
Dog's sound()and the output is:
Dog barksThis is called dynamic method dispatch.
The method that gets executed is determined at runtime based on the actual object.
super.method() and Method Overriding
Sometimes the child wants to use the parent’s implementation as well.
We can use super for this.
class Animal {void sound() {
System.out.println("Animal makes a sound");
}
}class Dog extends Animal { @Override
void sound() {
super.sound();
System.out.println("Dog barks");
}
}
Now:
Dog dog = new Dog();
dog.sound();Output:
Animal makes a sound
Dog barksHere:
super.sound();calls the parent’s version of the method.
Important Rules of Method Overriding
There are some important rules to remember:
1. Method signature should match
The child method should have the same method name and parameters as the parent method.
2. Return type
The return type should be the same, or a compatible covariant return type.
3. Access level cannot be more restrictive
For example, if the parent method is:
public void show()the child cannot override it as:
private void show()because that would reduce its accessibility.
4. final methods cannot be overridden
If we write:
final void show() {
}a child class cannot override that method.
5. static methods are not overridden
Static methods belong to the class rather than the object. If a child defines a static method with the same signature, it is called method hiding, not overriding.
Inheritance vs Method Overriding
These two concepts are related but different.
Inheritance means the child class gets access to properties and methods from the parent.
Parent
↓
ChildMethod overriding means the child provides a different implementation for an inherited method.
Parent
↓
Child → changes the implementationFor example:
class Animal {void sound() {
System.out.println("Animal sound");
}
}class Dog extends Animal { @Override
void sound() {
System.out.println("Dog barks");
}
}
Here:
extends→ inheritancesound()inDog→ method overriding@Override→ tells Java that we are overriding the parent methodsuper.sound()→ calls the parent implementation
One Simple Picture to Remember Everything
Object
|
Animal
|
Dog
/ \
inherits overrides
| |
sound() sound()So, the main idea is:
Inheritance allows a child class to reuse the properties and behaviours of a parent class, while method overriding allows the child class to provide its own implementation of an inherited method.
And remember the difference between this and super:
this() → calls another constructor of the same classsuper() → calls the constructor of the parent classthis.x → current class/object variablesuper.x → parent class variablethis.method() → current class methodsuper.method() → parent class method





