Link to home
Start Free TrialLog in
Avatar of kellyputty
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?
----------------------------------ERROR-------------------------------------
C:\java\code\c08>javac WindError.java
WindError.java:20: play(NoteX) in InstrumentX cannot be applied to (int)
    i.play(NoteX.MIDDLE_C);
-----------------------------------CODE--------------------------------------
//: 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("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);   }
} ///:~
Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

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
Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial