I've been going over Generic classes and methods in Java and came across this question in a past paper
I tried implementing the interface and classes proposed in the question, including the refuel method, and found no problem with passing the Car argument as a parameter
Motorized interface
public interface Motorized {
public int getFuel();
}
Vehicle class
public abstract class Vehicle{
private int fuel;
public Vehicle(int fuel) {
this.fuel = fuel;
}
public String toString(){
return("Fuel is: " + getFuel());
}
public int getFuel() {
return fuel;
}
}
Car class
public class Car extends Vehicle implements Motorized {
int seats;
public Car(int fuel, int seats) {
super(fuel);
this.seats = seats;
}
public int getSeats() {
return seats;
}
@Override
public String toString(){
return("Fuel is: " + getFuel() + "and the car has" + getSeats() + "seats.");
}
}
test method
public class VehicleTest {
public static Motorized refuel(Motorized v) {
return v;
}
public static void main(String[] args) {
Car car = new Car(15, 5);
System.out.println(refuel(car));
}
}
Can somebody explain to me what the problem should be with this question and why my implementation of it doesn't reflect this problem?
