kellyputty
asked on
Interface of constant values question
I am trying to get the following to compile. I am not sure how the syntax works when using an interface that only contains constant values.
The "play" method of both base class "InstrumentX" and derived class "WindX" use the constants in interface NoteX, however, since NoteX resolves to a constant int I get the following error.
How do I remedy the situation?
-------------------------- --------ER ROR------- ---------- ---------- ----------
C:\java\code\c08>javac WindError.java
WindError.java:20: play(NoteX) in InstrumentX cannot be applied to (int)
i.play(NoteX.MIDDLE_C);
-------------------------- ---------C ODE------- ---------- ---------- ---------- -
//: WindError.java
interface NoteX {
public static final int
MIDDLE_C = 0, C_SHARP = 1, C_FLAT = 2;
}
class InstrumentX implements NoteX {
public void play(NoteX n ) {
System.out.println("Instru mentX.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); }
} ///:~
The "play" method of both base class "InstrumentX" and derived class "WindX" use the constants in interface NoteX, however, since NoteX resolves to a constant int I get the following error.
How do I remedy the situation?
--------------------------
C:\java\code\c08>javac WindError.java
WindError.java:20: play(NoteX) in InstrumentX cannot be applied to (int)
i.play(NoteX.MIDDLE_C);
--------------------------
//: WindError.java
interface NoteX {
public static final int
MIDDLE_C = 0, C_SHARP = 1, C_FLAT = 2;
}
class InstrumentX implements NoteX {
public void play(NoteX n ) {
System.out.println("Instru
}
}
class WindX extends InstrumentX {
public void play(NoteX n ) {
System.out.println("WindX.
}
}
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); }
} ///:~
When you specify that a method accepts a parameter of type NoteX, you have to ensure that you pass a class instance which does just that.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
;-D