Skip to content

Inheritance in Java

Inheritance is one of the 4 pillars of object oriented programming. Using the concept of inheritance, a child class can inherit the properties and behaviour of a parent class(base class).


A simple inheritance example

public class Animal {
int age;
String name;
public void eat() {
System.out.println("The animal is eating....");
}
}
// Here Dog is the child class and Animal is the parent class.
public class Dog extends Animal {
public void bark() {
System.out.println("Dog in barking...");
}
}

When a child class(subclass) provides its own implementation of a method that already exists inside the parent class(superclass), that is called as Method Overriding. This is called runtime polymorphism — the JVM decides at runtime (not compile time) which version to run.

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog is barking");
}
}

super is a reference variable that always points to the immediate parent class of the current object. It is used to call the parent’s constructor, access its fields, or invoke its methods, even when the child has overridden them.

Use 1: super() to call the parent constructor

Section titled “Use 1: super() to call the parent constructor”
Computer.java
class Computer {
String brand;
Computer(String b) {
brand = b;
}
}
Laptop.java
class Laptop extends Computer {
String model;
Laptop(String b, String m) {
super(b); // Here the parent constructor has been called.
model = m;
}
}

Use 2: super.field to access a parent field

Section titled “Use 2: super.field to access a parent field”

This is used when the child has a field having the same name as the parent’s field (this is called field hiding). Without super we would only be able to see the child’s version.

class Animal {
String color = "White";
}
class Cat extends Animal {
String color = "Black";
void printColors() {
System.out.println(color); // -> "Black"
System.out.println(super.color); // -> "White"
}
}

Use 3: super.method() to call a parent method

Section titled “Use 3: super.method() to call a parent method”

This is the most important use of super. It can be used for calling a parent’s overridden method so you can extend its behavior rather than completely replace it.

class Animal {
void sound() {
System.out.println("Animal is making a sound.");
}
}
class Cat extends Animal {
@Override
void sound() {
super.sound();
System.out.println("Cat is meowing...");
}
}

Output

Animal is making a sound.
Cat is meowing...

Java has 5 types of inheritance. We will discuss each one by one.

This is the simplest type of inheritance. There is one parent and one child.

class A {}
class B extends A {}

There are multiple levels at which inheritance works here. Just like, grandparent -> parent -> child relation. Each level inherits everything from all the levels above it.

class A {}
class B extends A {}
class C extends B {}

One parent, multiple children. All children independently inherit from the same parent, but they don’t know about each other.

class A {}
class B extends A {}
class C extends A {}
class D extends A {}

A class inheriting from more than one parents. E.g. class Eagle inherits from both Animal and Bird classes.

A combination of two or more types of inheritance in the same program. For example: multilevel (class extends class) + multiple (class implements interfaces). Java allows this cleanly because interfaces prevent ambiguity.