Monday, July 27, 2009

Java Superclasses and Subclasses?

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??

Java Superclasses and Subclasses?
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

800flowers.com

No comments:

Post a Comment