โ† Back to Courses
ยฉ 2025 sampathirao yoganandh
Week 7 ยท CO-1

Inheritance & Polymorphism

Types of Inheritance, super, Method Overriding, Abstract Classes, Dynamic Dispatch  CO-1

Inheritance Overview

Inheritance enables one class to inherit fields and methods from another. It supports code reusability and establishes an IS-A relationship.

๐Ÿ“Œ Key PointJava does not support multiple inheritance through classes (to avoid the Diamond Problem), but it IS supported through interfaces.

Types of Inheritance

TypeDescriptionJava Support
SingleOne child inherits one parentโœ… Yes
Multi-levelAโ†’Bโ†’C (chain)โœ… Yes
HierarchicalOne parent, multiple childrenโœ… Yes
MultipleOne child, multiple parentsโŒ Via classes โ€” โœ… Via interfaces
HybridCombination of aboveโœ… Via interfaces
// Multi-level inheritance
class Animal   { void breathe() { System.out.println("Breathes"); } }
class Mammal   extends Animal  { void walk()    { System.out.println("Walks"); } }
class Dog      extends Mammal  { void bark()    { System.out.println("Barks"); } }

Dog d = new Dog();
d.breathe(); d.walk(); d.bark(); // all three accessible

super Keyword

The super keyword refers to the immediate parent class. It is used to:

  1. Access parent class fields (when shadowed by child)
  2. Call parent class methods (especially overridden ones)
  3. Call parent class constructor โ€” super() must be first line
class Shape {
    String color;
    Shape(String color) { this.color = color; }
    void draw() { System.out.println("Drawing " + color + " shape"); }
}

class Rectangle extends Shape {
    double width, height;

    Rectangle(String color, double w, double h) {
        super(color);      // call parent constructor
        width = w; height = h;
    }

    void draw() {
        super.draw();       // call parent method
        System.out.println("Rectangle: " + width + "x" + height);
    }
}

Method Overriding

Method Overriding occurs when a subclass provides its own implementation of a method already defined in the parent class. Rules:

class Bank {
    double getInterestRate() { return 4.0; }
}
class SBI extends Bank {
    @Override
    double getInterestRate() { return 7.0; }
}
class HDFC extends Bank {
    @Override
    double getInterestRate() { return 6.5; }
}

final with Inheritance

UsageEffect
final classClass cannot be extended at all
final methodMethod cannot be overridden in subclass
final variableActs as a constant; cannot be changed
๐Ÿ’ก Did You KnowThe String, Integer, and other wrapper classes are all declared final in Java's standard library.

Abstract Classes

An abstract class is a class that cannot be instantiated. It serves as a template, defining methods that subclasses must implement.

abstract class Vehicle {
    int speed;

    // abstract method โ€” no body
    abstract void fuelType();

    // concrete method
    void showSpeed() { System.out.println("Speed: " + speed); }
}

class Car extends Vehicle {
    @Override
    void fuelType() { System.out.println("Petrol / Diesel"); }
}

class ElectricCar extends Vehicle {
    @Override
    void fuelType() { System.out.println("Electric Battery"); }
}

Runtime Polymorphism

Dynamic Method Dispatch is the mechanism by which an overridden method is called through a parent class reference at runtime. The JVM determines which version to call based on the actual object type โ€” not the reference type.

public class Test {
    public static void main(String[] args) {
        Bank b;            // parent reference

        b = new SBI();
        System.out.println("SBI rate: "  + b.getInterestRate()); // 7.0

        b = new HDFC();
        System.out.println("HDFC rate: " + b.getInterestRate()); // 6.5

        // Array of Bank references โ†’ different objects
        Bank[] banks = { new SBI(), new HDFC(), new Bank() };
        for (Bank bank : banks)
            System.out.println(bank.getInterestRate()); // 7.0, 6.5, 4.0
    }
}
๐Ÿ“Œ Key PointRuntime polymorphism is one of the most powerful OOP features. It lets you write generic code that works with any subclass โ€” the correct version is chosen automatically at runtime.
๐Ÿ“„

Download PDF Notes

Save these notes as a PDF for offline reference

View / Download PDF