Write a program in C++ / Java using method overriding to calculate electricity bills for Residential and Commercial customers. Create a base class…

Write a program in C++ / Java using method overriding to calculate electricity bills for Residential and Commercial customers. Create a base class Customer with a method calculateBill(int units) and override it in the Residential class to charge 6 TK per unit and in the Commercial class to charge 10 TK per unit. In the main method, create objects of both classes, calculate the bills for given units, and display the results.

From the BPDB Assistant Engineer (CSE) October 2026 – Written (BUET) exam, question 29 of 30.

Answers (1)

Java, with the base method overridden in each subclass; the call goes to the object’s own class at run time:

class Customer {
    double calculateBill(int units) {
        return 0;
    }
}

class Residential extends Customer {
    @Override
    double calculateBill(int units) {
        return units * 6;   // 6 TK per unit
    }
}

class Commercial extends Customer {
    @Override
    double calculateBill(int units) {
        return units * 10;  // 10 TK per unit
    }
}

public class Main {
    public static void main(String[] args) {
        Customer residential = new Residential();
        Customer commercial = new Commercial();
        int units = 150;
        System.out.println("Residential bill: " + residential.calculateBill(units) + " TK");
        System.out.println("Commercial bill: " + commercial.calculateBill(units) + " TK");
    }
}

Output for 150 units: Residential bill: 900.0 TK, Commercial bill: 1500.0 TK.

— rhs

More from this paper