Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > polymorphic question

Reply
Thread Tools

polymorphic question

 
 
Khanh Le
Guest
Posts: n/a
 
      05-02-2004
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();
}
}


 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      05-02-2004
Khanh Le <> scribbled the following:
> 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 () ------------- 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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      05-02-2004
On 2 May 2004 10:23:28 GMT, Joona I Palaste <>
wrote or quoted :

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

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Tim Van Wassenhove
Guest
Posts: n/a
 
      05-02-2004
In article <Cu3lc.2621$. net>, Khanh Le wrote:

Multiposting is evil.

--
http://home.mysth.be/~timvw
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
polymorphic function question joblack Python 1 09-21-2010 06:39 PM
Polymorphic data type andy.wagg@gb.schneider-electric.com XML 2 02-19-2005 03:51 PM
SOAP: Creating a polymorphic Data Type andy.wagg@gb.schneider-electric.com Java 0 02-17-2005 01:54 PM
can webmethods be polymorphic? Manco ASP .Net 1 02-03-2005 08:59 AM
polymorphic behaviour from class constant Thomas Britton Java 1 05-02-2004 10:36 AM



Advertisments