suppose I have the following rough sketch of inheritance hierachy. When I
try to invoke the getSpeed() method at main(), I receive a "method not found
error", because the compiler keeps looking at the Vehicle class for the
getSpeed(), when instead it should look at the Car class for getSpeed().
Since Farrari and Ford extends Car and Car extends Vehicle, does it follow
that the compiler is supposed to search to the top of the the hierachy chain
beginning with the class at the bottom of the chain?
I had to use casting to solve this problem (casting from Vehicle to Car).
But I'd like to know why it doesn't work like I had intended it to.
Thanks
public class Vehicle
{
+public double Insurance()
}
public class Car extends Vehicle
{
+public int getSpeed()
}
public class Ferrari extends Car
{
private Speed = 200;
public Ferrari(){}
}
public class Ford extends Car
{
private speed = 160;
public Ford(){}
}
public class Test
{
public static void main(Sring[] args)
{
Vehicle [] v = new Vehicle[2];
v[0] = new Ferrari();
v[1] = new Ford();
int speed = v[0].getSpeed();
}
}