Back to the 2020 paper

Module III: Basics of Web Programming

20207m

What are the benefits of inheritance? Explain the various forms of inheritance with suitable code segments.

Worked SolutionAI Assisted

Answer: Benefits and Forms of Inheritance in Java

1. What is Inheritance?

Inheritance is a fundamental Object-Oriented Programming mechanism where a new class (Subclass / Child Class / Derived Class) derives properties, fields, and behaviors (methods) from an existing class (Superclass / Parent Class / Base Class) using the extends keyword. It models an "IS-A" relationship.


2. Benefits of Inheritance

  1. Code Reusability: Subclasses automatically inherit existing, tested methods from parent classes without re-writing code.
  2. Runtime Polymorphism: Enables dynamic method dispatch through method overriding (@Override), allowing polymorphic code behavior.
  3. Data Encapsulation & Hierarchy: Organizes classes into clear, logical, and maintainable domain hierarchies (e.g., Vehicle -> Car -> ElectricCar).
  4. Extensibility: Facilitates adding new features to existing software architectures without modifying original parent classes (Open-Closed Principle).

3. Various Forms of Inheritance in Java

  1. Single         2. Multilevel        3. Hierarchical      4. Multiple (Interfaces)
   +-----+             +-----+               +-----+               +-----+  +-----+
   |  A  |             |  A  |               |  A  |               | I1  |  | I2  |
   +--+--+             +--+--+               +--+--+               +--+--+  +--+--+
      |                   |                     |                     \       /
      v                   v                  +--+--+                   v     v
   +-----+             +-----+               |     |                   +-----+
   |  B  |             |  B  |               v     v                   |  C  |
   +-----+             +--+--+            +-----+ +-----+              +-----+
                          |               |  B  | |  C  |
                          v               +-----+ +-----+
                       +-----+
                       |  C  |
                       +-----+

Form 1: Single Inheritance (ABA \to B)

  • A single subclass inherits from a single superclass.
class Animal {
    void eat() { System.out.println("Animal eats food."); }
}

class Dog extends Animal {
    void bark() { System.out.println("Dog barks."); }
}

Form 2: Multilevel Inheritance (ABCA \to B \to C)

  • A subclass inherits from a derived class, forming an inheritance chain.
class Vehicle {
    void startEngine() { System.out.println("Engine started."); }
}

class Car extends Vehicle {
    void drive() { System.out.println("Car is driving on 4 wheels."); }
}

class ElectricCar extends Car {
    void chargeBattery() { System.out.println("Charging lithium-ion battery."); }
}

Form 3: Hierarchical Inheritance (ABA \to B and ACA \to C)

  • Multiple subclasses inherit from a single common superclass.
class Account {
    double balance;
    void showBalance() { System.out.println("Balance: " + balance); }
}

class SavingsAccount extends Account {
    double interestRate = 4.5;
}

class CurrentAccount extends Account {
    double overdraftLimit = 50000;
}

Form 4: Multiple Inheritance (Achieved via Interfaces)

  • Note on Classes: Java does not support multiple inheritance with classes (class C extends A, B) to prevent ambiguity and the "Diamond Problem".
  • Supported via Interfaces: A class can implement multiple interfaces using the implements keyword.
interface Printable {
    void print();
}

interface Showable {
    void show();
}

// Implementing multiple interfaces
class Document implements Printable, Showable {
    public void print() { System.out.println("Printing document page."); }
    public void show() { System.out.println("Showing document preview."); }
}

Form 5: Hybrid Inheritance

  • A combination of two or more types of inheritance (e.g., Multilevel + Multiple), implemented in Java using a mix of classes and interfaces.
interface GPS {
    void navigate();
}

class Phone {
    void makeCall() { System.out.println("Calling number..."); }
}

// Hybrid: Extends class Phone and Implements interface GPS
class SmartPhone extends Phone implements GPS {
    public void navigate() { System.out.println("Navigating via GPS maps."); }
}

Similar questions