Back to the 2020 paper

Module III: Basics of Web Programming

202014m

WAP to create a superclass called figure that stores the dimensions of a two-dimensional object. It also defines a method called area () that computes the area of an object. The program derives two subclasses from figure. The first is rectangle and the second is triangle. Each of these subclasses overrides area () so that it returns the area of a rectangle and a triangle respectively.

Worked SolutionAI Assisted

Answer: Java Program — Figure Hierarchy with Dynamic Method Dispatch

1. Problem Specification

  • Superclass: Figure
    • Instance variables: dim1, dim2 (dimensions of a 2D geometric object).
    • Method: area() returning double.
  • Subclasses:
    • Rectangle: Overrides area() to compute Length×Breadth=dim1×dim2\text{Length} \times \text{Breadth} = \text{dim1} \times \text{dim2}.
    • Triangle: Overrides area() to compute 12×Base×Height=0.5×dim1×dim2\frac{1}{2} \times \text{Base} \times \text{Height} = 0.5 \times \text{dim1} \times \text{dim2}.
  • Driver Class: Demonstrates Dynamic Method Dispatch (Runtime Polymorphism).
                           +------------------------+
                           |         Figure         |
                           |------------------------|
                           | double dim1, dim2      |
                           | double area()          |
                           +-----------+------------+
                                       |
                     +-----------------+-----------------+
                     |                                   |
                     v                                   v
             +---------------+                   +---------------+
             |   Rectangle   |                   |   Triangle    |
             |---------------|                   |---------------|
             | double area() |                   | double area() |
             | (dim1 * dim2) |                   | (0.5*dim1*dim2|
             +---------------+                   +---------------+

2. Complete Java Program

// Superclass Figure
class Figure {
    double dim1;
    double dim2;

    // Parameterized constructor to initialize dimensions
    Figure(double dim1, double dim2) {
        this.dim1 = dim1;
        this.dim2 = dim2;
    }

    // Base area method (to be overridden by subclasses)
    double area() {
        System.out.println("Area for generic Figure is undefined.");
        return 0;
    }
}

// Subclass Rectangle overriding area()
class Rectangle extends Figure {
    Rectangle(double length, double breadth) {
        super(length, breadth); // Pass dimensions to superclass
    }

    // Overriding area() for rectangle: length * breadth
    @Override
    double area() {
        System.out.println("Calculating Area of Rectangle:");
        return dim1 * dim2;
    }
}

// Subclass Triangle overriding area()
class Triangle extends Figure {
    Triangle(double base, double height) {
        super(base, height); // Pass dimensions to superclass
    }

    // Overriding area() for triangle: 1/2 * base * height
    @Override
    double area() {
        System.out.println("Calculating Area of Triangle:");
        return 0.5 * dim1 * dim2;
    }
}

// Driver Class to test polymorphism
public class FigureAreaDemo {
    public static void main(String[] args) {
        // Instantiate specific shape objects
        Figure genericFig = new Figure(10, 10);
        Rectangle rect = new Rectangle(12.0, 8.0);
        Triangle tri = new Triangle(10.0, 6.0);

        // Reference variable of superclass Figure
        Figure figRef;

        System.out.println("========================================");
        System.out.println("   DYNAMIC METHOD DISPATCH DEMO");
        System.out.println("========================================
");

        // 1. Superclass reference pointing to Rectangle object
        figRef = rect;
        System.out.println("Rectangle Dimensions: [Length = " + rect.dim1 + ", Breadth = " + rect.dim2 + "]");
        System.out.println("Computed Area = " + figRef.area() + " sq units
");

        // 2. Superclass reference pointing to Triangle object
        figRef = tri;
        System.out.println("Triangle Dimensions: [Base = " + tri.dim1 + ", Height = " + tri.dim2 + "]");
        System.out.println("Computed Area = " + figRef.area() + " sq units
");

        // 3. Superclass reference pointing to generic Figure object
        figRef = genericFig;
        System.out.println("Generic Figure Area = " + figRef.area());
    }
}

3. Sample Output

========================================
   DYNAMIC METHOD DISPATCH DEMO
========================================

Rectangle Dimensions: [Length = 12.0, Breadth = 8.0]
Calculating Area of Rectangle:
Computed Area = 96.0 sq units

Triangle Dimensions: [Base = 10.0, Height = 6.0]
Calculating Area of Triangle:
Computed Area = 30.0 sq units

Generic Figure Area = Area for generic Figure is undefined.
0.0

4. Key OOP Concepts Demonstrated

  1. Inheritance (extends & super): The subclasses inherit fields dim1 and dim2 and pass values using super(dim1, dim2).
  2. Method Overriding (@Override): Subclasses provide specific implementations of the inherited area() method.
  3. Runtime Polymorphism / Dynamic Method Dispatch: The Java Virtual Machine resolves the call figRef.area() dynamically at runtime based on the actual object type referenced by figRef, rather than the reference type.

Similar questions