Question 2
Consider the code given below.
interface Vehicle {
public abstract double getFuelEfficiency();
}
class Car implements Vehicle, Cloneable {
protected double fuelCapacity;
protected double mileage;
public Car(double fuel, double m) {
fuelCapacity = fuel;
mileage = m;
}
public double getFuelEfficiency() {
return fuelCapacity * mileage;
}
public Car clone() throws CloneNotSupportedException {
return (Car) super.clone();
}
}
class ElectricCar extends Car {
public ElectricCar(double batteryCapacity, double mileage) {
super(batteryCapacity, mileage);
}
public ElectricCar clone() throws CloneNotSupportedException {
return (ElectricCar) super.clone();
}
}
public class VehicleTest {
public static void main(String[] args) {
try {
ElectricCar e1 = new ElectricCar(50, 2);
ElectricCar e2 = e1.clone();
e1.fuelCapacity = 100;
System.out.print(e1.getFuelEfficiency() + e2.getFuelEfficiency());
} catch (CloneNotSupportedException e) {
System.out.println("Cloning not supported");
}
}
}
What will the output be?
200.0
300.0
400.0
Cloning not supported