polymorphic question

Discussion in 'Java' started by Khanh Le, May 2, 2004.

  1. Khanh  Le

    Khanh Le Guest

    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();
    }
    }
     
    Khanh Le, May 2, 2004
    #1
    1. Advertising

  2. 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
     
    Joona I Palaste, May 2, 2004
    #2
    1. Advertising

  3. Khanh  Le

    Roedy Green Guest

    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.
     
    Roedy Green, May 2, 2004
    #3
  4. Tim Van Wassenhove, May 2, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manco

    can webmethods be polymorphic?

    Manco, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    325
    Lionel LASKE
    Feb 3, 2005
  2. Thomas Britton

    polymorphic behaviour from class constant

    Thomas Britton, May 1, 2004, in forum: Java
    Replies:
    1
    Views:
    335
    Chris Uppal
    May 2, 2004
  3. -electric.com

    SOAP: Creating a polymorphic Data Type

    -electric.com, Feb 17, 2005, in forum: Java
    Replies:
    0
    Views:
    391
    -electric.com
    Feb 17, 2005
  4. -electric.com

    Polymorphic data type

    -electric.com, Feb 18, 2005, in forum: XML
    Replies:
    2
    Views:
    731
  5. joblack

    polymorphic function question

    joblack, Sep 21, 2010, in forum: Python
    Replies:
    1
    Views:
    254
    Gregor Horvath
    Sep 21, 2010
Loading...

Share This Page