Hi I am having trouble with my code (it isn\'t the whole thing only part of it).
ID: 3862594 • Letter: H
Question
Hi I am having trouble with my code (it isn't the whole thing only part of it). However, I am having issues with running methods in the parent classes when I call from the subclasses. It won't let me use the methods and I don't know how to extend them to the subclasses. I'd appreciate if someone could help.
class main {
public static void main ( String [] args)
{ Vehicle [] vehiclesArray = new Vehicle[100];
switch (type)
{ case 't': vehiclesArray[index] = new Truck();
vehiclesArray[index].setDriverType(driverType);
break;
case 'v':
vehiclesArray[index] = new Van();
vehiclesArray[index].setDriverType(driverType);
vehiclesArray[index].setVanType(vanType);
break;
case 's':
vehiclesArray[index] = new SailBoat();
vehiclesArray[index].setNumSails(numSails);
vehiclesArray[index].setBoatLength(boatLength);
break;
case 'p':
vehiclesArray[index] = new PowerBoat();
vehiclesArray[index].setEngineType(engineType);
vehiclesArray[index].setBoatLength(boatLength);
break;
default:
break; }
vehiclesArray[index].setMake(make);
vehiclesArray[index].setModel(model);
vehiclesArray[index].setYear(year);
vehiclesArray[index].setSerialNum(serialNum);
} //end main
public class Vehicle {
private String type; private String make; private String serialNum; private String model; private int year;
public Vehicle() { }
public void setType(char newType) {
if (newType == 't')
{ type = "Truck"; }
if (newType == 'v')
{ type = "Van"; }
if (newType == 's')
{ type = "SailBoat"; }
if (newType == 'p')
{ type = "PowerBoat"; } }
public String getType() {
return type; }
public String getMake()
{ return make; }
public String getSerialNum()
{ return serialNum; }
public String getModel()
{ return model; }
public int getYear()
{ return year; }
//mutators
public void setMake(String newMake)
{ make = newMake; }
public void setSerialNum(String newSerialNum)
{ serialNum = newSerialNum; }
public void setModel(String newModel)
{ model = newModel; }
public void setYear(int newYear)
{ year = newYear; }
public String toString()
{ return "Type: " + type + " make: " + make + " model: " + model + " year: " + year + " serial number: " + serialNum; }
} //end class
public class Car extends Vehicle {
private String driverType;
public Car() {
super(); }
public String getDriverType() {
return driverType;
}
public void setDriverType(String newDriverType)
{
driverType = newDriverType;
}
public String toSting()
{
return " driver type: " + driverType;
}
}
Van extends Car {
private String vanType;
public Van()
{ super(); }
public String getVanType()
{ return vanType; }
public void setVanType(String newVanType)
{ vanType = newVanType; }
public String toString()
{ return " van type: " + vanType; }
} //end class
Explanation / Answer
Hi,
If you are facing issue in the lines here down:
For better understanding, I have taken only some lines of your code.
------------------------------------------------------------------------------------
Vehicle [] vehiclesArray = new Vehicle[100]; //Creating Parent class object
vehiclesArray[index] = new Van(); // Creating "van" class object which is subclass of Vehicle and storing into Vehicle class object array
vehiclesArray[index].setVanType(vanType);// Getting error in this line "The method setVanType(String) is undefined for the type Vehicle"
// vehiclesArray[index] is an element of Vehicle class object
// setVanType() is the method of class "van"
------------------------------------------------------------------------------------
In Java inheritance concept, Subclass methods cannot be called using Parent class object. Thats why you are getting error in all those lines.
Example: Take Father and son
*Son can have some properties of father
*But No vice versa
Please let me know for more help.