A ride-sharing application supports cars, motorbikes, and CNG auto-rickshaws, each with a different fare calculation method. Design the system using…

A ride-sharing application supports cars, motorbikes, and CNG auto-rickshaws, each with a different fare calculation method. Design the system using appropriate object-oriented programming (OOP) principles and draw a UML class diagram illustrating your design.

From the Combined Bank Assistant Programmer (CSE) October 2026 – Written (BIBM) exam, question 9 of 21.

Answers (1)

Design. The fare differs by vehicle type, so Vehicle is an abstract class with an abstract method calculateFare(). Car, Motorbike and CngAutoRickshaw extend it and each overrides the method. A Ride asks its driver’s vehicle for the fare without knowing the concrete type.

OOP principles used

  • Abstraction: Vehicle defines what every vehicle can do (calculateFare) without saying how.
  • Inheritance: the three types share regNo, baseFare and perKmRate from Vehicle.
  • Polymorphism: ride.fare() calls vehicle.calculateFare(); the right version runs at run time (method overriding).
  • Encapsulation: fields are private/protected, accessed only through methods.
  • Open–closed principle: a new type (e.g. Micro or Ambulance) is a new subclass; Ride does not change.

Code sketch (Java; example rates)

abstract class Vehicle {
    protected String regNo;
    protected double baseFare, perKmRate;
    Vehicle(String regNo, double baseFare, double perKmRate) {
        this.regNo = regNo; this.baseFare = baseFare; this.perKmRate = perKmRate;
    }
    abstract double calculateFare(double km, double min);
}

class Car extends Vehicle {
    private double perMinRate = 2;
    Car(String reg) { super(reg, 50, 30); }
    double calculateFare(double km, double min) {
        return baseFare + perKmRate * km + perMinRate * min;
    }
}

class Motorbike extends Vehicle {
    Motorbike(String reg) { super(reg, 20, 15); }
    double calculateFare(double km, double min) {
        return baseFare + perKmRate * km;
    }
}

class CngAutoRickshaw extends Vehicle {
    private double minimumFare = 40;
    CngAutoRickshaw(String reg) { super(reg, 25, 20); }
    double calculateFare(double km, double min) {
        return Math.max(minimumFare, baseFare + perKmRate * km);
    }
}

class Ride {
    private Rider rider; private Driver driver;
    private double distanceKm, durationMin;
    double fare() {
        return driver.getVehicle().calculateFare(distanceKm, durationMin);
    }
}

UML class diagram (attached image): Rider 1 — 0..* Ride (books); Driver 1 — 0..* Ride (drives); Driver ◇— Vehicle (aggregation, has one); Ride ⇢ Vehicle (uses calculateFare); Car, Motorbike and CngAutoRickshaw inherit from the abstract Vehicle (hollow triangle).

— rhs

More from this paper