polymorphic question

K

Khanh Le

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

Joona I Palaste

Khanh Le said:
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?

No. The compiler is only concerned about the compile-time types of
the object references.
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.

Suppose you had this kind of code...
Vehicle v;
if (someRunTimeFlag) {
v = new Vehicle();
}
else {
v = new Ferrari();
}
v.getSpeed();
Now is the compiler supposed to somehow see into the future and
predict whether someRunTimeFlag will be true or not?
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();
}
}

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
 
R

Roedy Green

Vehicle v;
if (someRunTimeFlag) {
v = new Vehicle();
}
else {
v = new Ferrari();
}
v.getSpeed();
Now is the compiler supposed to somehow see into the future and
predict whether someRunTimeFlag will be true or not?

Since Ferrari is derived from Vehicle, the compiler only assumes that
v is a Vehicle and Ferrari is a type of Vehicle. IT will use look at
a table AT RUN TIME to find the getSpeed method for Ferraris, with may
be the same as for generic vehicles or it may be overridden.

See http://mindprod.com/jgloss/gotchas.html
and search for recipe to get a handle on how this overriding business
works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,614
Members
45,292
Latest member
EttaCasill

Latest Threads

Top