473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Polymorphism question

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
2 2781
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mayer Goldberg | last post by:
Can someone please explain the motivation behind the following restriction in the language: I define an interface and two classes implementing it: public interface InterA {} public class B...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
3
by: John Salerno | last post by:
Along with events and delegates, polymorphism has been something I sort of struggle with every now and then. First, let me quote the book I'm reading: "Polymorphism is most useful when you have...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.