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).
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..."); }}1. Method Overriding
Section titled “1. Method Overriding”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"); }}2. super keyword
Section titled “2. super keyword”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”class Computer { String brand; Computer(String b) { brand = b; }}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...Types of inheritance in java
Section titled “Types of inheritance in java”Java has 5 types of inheritance. We will discuss each one by one.
1. Single Inheritance
Section titled “1. Single Inheritance”This is the simplest type of inheritance. There is one parent and one child.
class A {}class B extends A {}2. Multilevel Inheritance
Section titled “2. Multilevel Inheritance”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 {}3. Hierarchial Inheritance
Section titled “3. Hierarchial Inheritance”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 {}4. Multiple Inheritance
Section titled “4. Multiple Inheritance”A class inheriting from more than one parents.
E.g. class Eagle inherits from both Animal and Bird classes.
5. Hybrid Inheritance
Section titled “5. Hybrid Inheritance”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.