By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,739 Members | 2,014 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,739 IT Pros & Developers. It's quick & easy.

Polymorphism question

P: n/a
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();
}
}




Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,
public static void main(Sring[] args)
{
Vehicle [] v = new Vehicle[2];
v[0] = new Ferrari();
v[1] = new Ford();

int speed = v[0].getSpeed();
}


It looks perfectly right with me that your compiler complains about a
missing getSpeed method in something that you typed as a Vehicle: There
IS no getSpeed method in Vehicle. The only thing the COMPILER knows
about you array is that there are Vehichles, or some subtype, in it.
At RUN-TIME, you actually may have subtypes in the array that HAVE a
getSpeed method. But you did not promise the compiler that (give the
array a Car[] type, or subtype).

Solutions:
If the Vehicles in the array are always Cars anyway, make the array a
Car[] array

or

Do the cast thing that you are doing

or

if (foo[i] instanceof Car) {
Car c = (Car) foo[i];
c. ....... blah blah
} else {} // ignore non Cars

or

Add a getSpeed method to Vehicle, and have it throw some home made
DontTouchMeException. It really just is deferring the type problem to
run-time, but sometimes that kind of solution is the only acceptable one.

Soren
Jul 17 '05 #2

P: n/a
you don't understand virtual method invocation or syntax

- perry

Khanh Le wrote:

public class Vehicle
{
+public double Insurance() {};
}
public class Car extends Vehicle
{
+public int getSpeed() {} virtual public int getSpeed() {}; }

public class Ferrari extends Car
{
private Speed = 200;
public Ferrari(){} virtual public int getSpeed() { return Speed; }; }

public class Ford extends Car
{
private speed = 160;
public Ford(){} virtual public int getSpeed() { return speed; }; }

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();
}
}




Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.