Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

  • it is an oop concept that allows a class to inherit properties and behaviors from another class.
  • It promotes code reusability, modularity and establishes a hierarchical relationship between classes
  • the extends keyword is used to establish inheritance.
class Superclass {
    // Fields and methods
}
class Subclass extends Superclass {
    // Additional fields and methods, or overrides
}
  • purpose :
    • reusability - reuse existing code from superclass.
    • extensibiliy - add new features or modify inherited behavior in the subclass.
    • polymorphism - enable polymorphic behavior where a superclass reference can point to a subclass object.
class Animal {
    String name;
    
    Animal(String name) {
        this.name = name;
    }
    
    void eat() {
        System.out.println(name + " is eating.");
    }
}

class Dog extends Animal {
    Dog(String name) {
        super(name); // Call superclass constructor
    }
    
    // Override eat method
    @Override
    void eat() {
        System.out.println(name + " is eating bones.");
    }
    
    // New method
    void bark() {
        System.out.println(name + " is barking.");
    }
}

class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy");
        dog.eat();  // Output: Buddy is eating bones.
        dog.bark(); // Output: Buddy is barking.
    }
}

types of inheritance

  • single inheritance - a subclass inherits from a superclass
class A {}
class B extends A {} // B inherits from A
  • multilevel inheritance - a class inherits from a superclass which further inherits from another class.
class A {}
class B extends A {}
class C extends B {} // C inherits from B, which inherits from A
  • hierarchical inheritance - multiple classes inherit from the same subclass``
class A {}
class B extends A {}
class C extends A {} // B and C both inherit from A
  • multiple inheritance - a class inherits from multiple super classes (is not supported in java via class). is done using interfaces
interface I1 {}
interface I2 {}
class C implements I1, I2 {} // Valid
  • hybrid inheritance - a combination of two or more types of inheritance.

limitations

  • no multiple inheritance
  • tight coupling - overuse of inheritance can lead to tightly coupled code leading to harder maintainability
  • fragile base case problem - changes to the superclass can unintentionally break subclasses
class Vehicle {
    String brand;
    int speed;
    
    Vehicle(String brand, int speed) {
        this.brand = brand;
        this.speed = speed;
    }
    
    void move() {
        System.out.println(brand + " is moving at " + speed + " km/h.");
    }
}

class Car extends Vehicle {
    int doors;
    
    Car(String brand, int speed, int doors) {
        super(brand, speed);
        this.doors = doors;
    }
    
    @Override
    void move() {
        System.out.println(brand + " car with " + doors + " doors is moving at " + speed + " km/h.");
    }
}

class Main {
    public static void main(String[] args) {
        Vehicle vehicle = new Car("Toyota", 120, 4);
        vehicle.move(); // Output: Toyota car with 4 doors is moving at 120 km/h.
    }
}