Java inheritance question
Posted
by Milos
on Stack Overflow
See other posts from Stack Overflow
or by Milos
Published on 2010-04-13T01:44:40Z
Indexed on
2010/04/13
1:52 UTC
Read the original article
Hit count: 382
I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend class Airplane.
I also have an interface Measurable, and two classes that implement it - People and Containers.
So, Airplane can do many things on its own, and there is a method which allows measurable things to be added to the airplane (called addAMeasurableThing). The only difference between PassengerAirplane/CargoAirplane and just an Airplane is that addAMeasurableThing should only accept People / Containers, and not any kind Measurable things. How do I implement this?
I tried doing:
Airplane class:
public abstract Airplane addAMeasurableThing (Measurable m, int position);
PassengerAirplane class:
public Airplane addAMeasurableThing (Measurable m, int position) { if (m instanceof People)...
CargoAirplane class:
public Airplane addAMeasurableThing (Measurable m, int position) { if (m instanceof Containers)...
But when I was debugging it, I've noticed that addAMeasurableThing in the CargoAirplane class never gets called, because both methods have the same signature. So how can the appropriate PassengerAirplane/CargoAirplane's addAMeasurableThing be called, depending on the type of Measurable thing that is being passed on?
Thanks!
© Stack Overflow or respective owner