The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Overloading with Inheritance

Posted by Anish on November 28, 2001 at 1:34 PM

Is it not possible to combine overloading with inheritance?
Take the following code from BruceEckel's Thinking in Java Edition 1, p263. He seems to suggest that overloading with inheritance is possible. I ran the following program and it seemed to work fine and called up the play() method in InstrumentX. Now I wanted to see if I could call up the play method in WindX instead. So I changed line 20 to "i.play(new NoteX());" thus passing play() a NoteX instead of an int. But I keep getting an error. Whats wrong?

//WindError.java
class NoteX {
public static final int
MIDDLE_C = 0, C_SHARP = 1, C_FLAT = 2;
}

class InstrumentX {
public void play(int noteX) {
System.out.println("InstrumentX.play()");
}
}

class WindX extends InstrumentX {
public void play(NoteX n) {
System.out.println("WindX.play(NoteX n)");
}
}

public class WindError {
public static void tune(InstrumentX i) {
i.play(NoteX.MIDDLE_C);
}
public static void main(String[] args) {
WindX flute = new WindX();
tune(flute);
}
}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us