English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have an abstract superclass Vehicle, derived class Car, and derived class CompactCar:

public abstract class Vehicle
{

public Vehicle(String Model, int Mileage, String VIN)
{
pModel = Model;
pMileage = Mileage;
pVIN = VIN;
}

//abstract public String getVehicleInfo();
abstract public void setReservation(boolean reservation);
abstract public boolean getReservation();
abstract public String getVehicleType();
abstract public String getVehicleNumber();
abstract public int getMileage();

//private data members
private int pNumberOfSeats;
private boolean pIsReserved;
private String pVIN;
private int pMileage;
}

//car class
public Car(String Model, int Mileage, String VIN)
{
super(Model, Mileage, VIN);
pVIN = VIN;
pModel = Model;
pMileage = Mileage;
}

//driver program
Car c = new CompactCar("Nissan", 20, "23537HYHU5");
int i = 0;
i = c.getMileage();

This keeps throwing an error saying no identifier in the above line.

How do I fix this??

2006-10-01 14:55:00 · 1 answers · asked by The Disciple 2 in Computers & Internet Programming & Design

1 answers

In your Car class, override the inherited (but abstract) method getMileage(). Your Car class is inheriting an abstract method with no body. By adding the method to Car, you are overriding the inherited behavior (which is non-existant since it happens to be abstract).

//car class
public Car(String Model, int Mileage, String VIN)
{
super(Model, Mileage, VIN);
pVIN = VIN;
pModel = Model;
pMileage = Mileage;
}

//overridden method with body
public int getMileage(){
return pMileage;
}

//driver program
Car c = new CompactCar("Nissan", 20, "23537HYHU5");
int i = 0;
i = c.getMileage(); //should return 20

2006-10-01 19:12:47 · answer #1 · answered by vincentgl 5 · 1 0

fedest.com, questions and answers