The Artima Developer Community
Sponsored Link

Java Answers Forum
WindError.java solution(s)

2 replies on 1 page. Most recent reply: Jan 18, 2003 9:32 PM by Janet Brown

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Janet Brown

Posts: 2
Nickname: peripheral
Registered: Jan, 2003

WindError.java solution(s) Posted: Jan 18, 2003 5:50 PM
Reply to this message Reply
Advertisement
I understand that the following source demonstrates that an override was intended but inadvertently an overload was implemented. How does one fix the problem?

//: c07:WindError.java
// Accidentally changing the interface.

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 {
// OOPS! Changes the method interface:
public void play(NoteX n) {
System.out.println("WindX.play(NoteX n)");
}
}


Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: WindError.java solution(s) Posted: Jan 18, 2003 7:44 PM
Reply to this message Reply
> I understand that the following source demonstrates that
> an override was intended but inadvertently an overload was
> implemented. How does one fix the problem?
>
> //: c07:WindError.java
> // Accidentally changing the interface.
>
> 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 {
> // OOPS! Changes the method interface:
> public void play(NoteX n) {
> System.out.println("WindX.play(NoteX n)");
> }
> }

class WindX extends InstrumentX {
// OOPS! Changes the method interface:
public void play(int NoteX) {
System.out.println("super.play(n)");
}
}


However, I think the method parameter should be of type NoteX instead of int (In InstrumentX).

Janet Brown

Posts: 2
Nickname: peripheral
Registered: Jan, 2003

Re: WindError.java solution(s) Posted: Jan 18, 2003 9:32 PM
Reply to this message Reply
Sorry, when I posted the original message, when I pasted it didn't add the last part of the source code for some reason. I don't know if this makes a difference. After:
class WindX extends InstrumentX {
// OOPS! Changes the method interface:
public void play(NoteX n) {
System.out.println("WindX.play(NoteX n)");
}
}
The code should read:
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); // Not the desired behavior!
}
} ///:~
Let me know if this changes your answer. Thanks for your help!

Flat View: This topic has 2 replies on 1 page
Topic: Network Design Question Previous Topic   Next Topic Topic: how to devide the stream?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use