Back to the 2019 paper

Module III: Basics of Web Programming

20197m

What is Inheritance? Explain its different types. Write Java program to implement multiple inheritance.

Worked SolutionAI Assisted

Answer: Inheritance Types and Multiple Inheritance Implementation in Java

1. What is Inheritance?

Inheritance is an object-oriented programming feature where a new class (subclass / derived class) acquires the properties, fields, and methods of an existing class (superclass / base class) using the extends keyword.

  • It models an "IS-A" relationship (e.g., Car IS-A Vehicle).
  • Enables code reusability and runtime polymorphism (method overriding).

2. Types of Inheritance in Java

 1. Single             2. Multilevel            3. Hierarchical
  +-------+              +-------+                 +-------+
  | Base  |              | Grand |                 | Base  |
  +---+---+              +---+---+                 +---+---+
      |                      |                         |
      v                      v                      +--+--+
  +-------+              +-------+                  |     |
  |Derived|              |Parent |                  v     v
  +-------+              +---+---+               +----+ +----+
                             |                   |Sub1| |Sub2|
                             v                   +----+ +----+
                         +-------+
                         | Child |
                         +-------+
  1. Single Inheritance: A single subclass extends a single superclass.
  2. Multilevel Inheritance: A class extends a derived class, creating an inheritance chain (ABCA \to B \to C).
  3. Hierarchical Inheritance: Multiple subclasses inherit from a single common superclass.
  4. Multiple Inheritance: A class inherits from more than one superclass. (Supported in Java through Interfaces, but not through classes).
  5. Hybrid Inheritance: A combination of two or more types of inheritance.

3. Why Multiple Inheritance is Disallowed in Java with Classes

Java does not support multiple inheritance with classes (class C extends A, B) to prevent ambiguity known as the "Diamond Problem":

           +-------+
           | ClassA| (defines display())
           +---+---+
               |
         +-----+-----+
         |           |
      +--v----+   +--v----+
      |ClassB |   |ClassC | (Both override display())
      +--+----+   +--+----+
         |           |
         +-----+-----+
               |
           +---v---+
           |ClassD | (Which display() should ClassD inherit? Ambiguity!)
           +-------+

Java resolves this cleanly by allowing multiple inheritance only through Interfaces, since interface methods traditionally have no conflicting state.


4. Java Program Implementing Multiple Inheritance via Interfaces

// Interface 1: Backend Server Interface
interface BackendEngine {
    void processData();
    void connectDatabase();
}

// Interface 2: Frontend UI Interface
interface UserInterface {
    void renderUI();
}

// Subclass implementing multiple interfaces
class WebApplication implements BackendEngine, UserInterface {
    private String appName;

    WebApplication(String name) {
        this.appName = name;
    }

    // Implementing BackendEngine methods
    @Override
    public void processData() {
        System.out.println("[" + appName + "] Processing business logic on server.");
    }

    @Override
    public void connectDatabase() {
        System.out.println("[" + appName + "] Database connected successfully.");
    }

    // Implementing UserInterface methods
    @Override
    public void renderUI() {
        System.out.println("[" + appName + "] Rendering responsive HTML5 interface.");
    }
}

// Driver Class
public class MultipleInheritanceDemo {
    public static void main(String[] args) {
        WebApplication portal = new WebApplication("PYQDeck Portal");

        System.out.println("--- Executing WebApplication Capabilities ---");
        portal.connectDatabase();
        portal.processData();
        portal.renderUI();
    }
}

Program Output:

--- Executing WebApplication Capabilities ---
[PYQDeck Portal] Database connected successfully.
[PYQDeck Portal] Processing business logic on server.
[PYQDeck Portal] Rendering responsive HTML5 interface.

Similar questions