⬅ Previous Next ➡

Inheritance & Polymorphism

Inheritance in Java: Types, Overloading/Overriding, super, Dynamic Dispatch, final & instanceof
  • Inheritance allows a class (child) to acquire properties and methods of another class (parent).
  • It improves code reusability and supports polymorphism.

1) Types of Inheritance (Java)

  • Single Inheritance: one child inherits one parent.
  • Multilevel Inheritance: chain of inheritance (A → B → C).
  • Hierarchical Inheritance: multiple children inherit one parent.
  • Multiple Inheritance (not supported with classes): Java does not allow multiple inheritance using classes to avoid ambiguity. It is achieved using interfaces.
  • Hybrid: combination of types (possible using interfaces).
// Single Inheritance
class Parent {
    void show() {
        System.out.println("Parent show()");
    }
}
class Child extends Parent {
    void childOnly() {
        System.out.println("Child childOnly()");
    }
}

// Multilevel Inheritance
class A {
    void a() { System.out.println("A"); }
}
class B extends A {
    void b() { System.out.println("B"); }
}
class C extends B {
    void c() { System.out.println("C"); }
}

// Hierarchical Inheritance
class Base {
    void common() { System.out.println("Common"); }
}
class One extends Base { }
class Two extends Base { }

2) Method Overloading

  • Overloading means same method name with different parameters (compile-time polymorphism).
  • Can differ by number of arguments, type of arguments, or order of arguments.
  • Return type alone cannot overload a method.
class OverloadDemo {
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }

    public static void main(String[] args) {
        OverloadDemo d = new OverloadDemo();
        System.out.println(d.add(10, 20));
        System.out.println(d.add(10.5, 20.2));
        System.out.println(d.add(1, 2, 3));
    }
}

3) Method Overriding

  • Overriding means child class provides a new implementation of parent method (runtime polymorphism).
  • Same method name + same parameters + same return type (or covariant).
  • Requires inheritance.
class Vehicle {
    void run() {
        System.out.println("Vehicle is running");
    }
}

class Bike extends Vehicle {
    @Override
    void run() {
        System.out.println("Bike is running");
    }

    public static void main(String[] args) {
        Bike b = new Bike();
        b.run();
    }
}

4) super Keyword

  • super refers to immediate parent class object.
  • Used to access parent variable/method, or call parent constructor using super().
class Person {
    String name = "ParentName";

    Person() {
        System.out.println("Person constructor");
    }

    void show() {
        System.out.println("Person show()");
    }
}

class Student extends Person {
    String name = "ChildName";

    Student() {
        super(); // calls parent constructor
        System.out.println("Student constructor");
    }

    void show() {
        super.show(); // calls parent method
        System.out.println("Student show()");
        System.out.println("Parent name: " + super.name);
        System.out.println("Child name: " + this.name);
    }

    public static void main(String[] args) {
        Student s = new Student();
        s.show();
    }
}

5) Dynamic Method Dispatch (Runtime Polymorphism)

  • When a parent reference points to a child object, and overridden method is called.
  • The method executed is decided at runtime.
class App {
    void open() {
        System.out.println("Open App");
    }
}

class WebApp extends App {
    @Override
    void open() {
        System.out.println("Open Web App");
    }

    public static void main(String[] args) {
        App a = new WebApp(); // parent reference -> child object
        a.open(); // calls WebApp open() at runtime
    }
}

6) final Keyword

  • final variable: value cannot be changed (constant).
  • final method: cannot be overridden.
  • final class: cannot be inherited.
final class SecureApp {
    final int MAX_LOGIN = 3; // final variable

    final void showLimit() { // final method
        System.out.println("Max Login: " + MAX_LOGIN);
    }
}

// class Test extends SecureApp { } // ERROR: cannot inherit final class

7) instanceof Operator

  • Used to check whether an object is an instance of a class or subclass.
  • Returns true or false.
class InstanceOfDemo {
    public static void main(String[] args) {
        String name = "Sourav";

        System.out.println(name instanceof String);   // true
        System.out.println(name instanceof Object);   // true (String is Object)

        Object obj = new StringBuilder("Java");
        System.out.println(obj instanceof StringBuilder); // true
        System.out.println(obj instanceof String);        // false
    }
}
⬅ Previous Next ➡