I am working on an assignment to implement an interface, but can't seem to work. It is taking forever to get a response back from the instructor, so I am hoping someone here can help me out.

The book shows some syntax on how to implment an interface, but I can't get my code to work the same way. My code below will compile fine, but from what I grasped of the use of interfaces, my code isn't implmenting a true interface (at least not one that really provides any functionality). The commented out lines are what I was trying to add to the interface... can some one point out my error?

public class StockApp implements StockWatcher {
public static void valueChanged(String tickerSymbol, double newValue) {
if (tickerSymbol.equals(sunTicker)) {
System.out.println("sunTicker...");
System.out.println(tickerSymbol + " stock just went up $" + newValue);
} else if (tickerSymbol.equals(oracleTicker)) {
System.out.println("oracleTicker...");
System.out.println(tickerSymbol + " stock just went up $" + newValue);
} else if (tickerSymbol.equals(ciscoTicker)) {
System.out.println("ciscoTicker...");
System.out.println(tickerSymbol + " stock just went up $" + newValue);
}
}
public static void currentValue(String tickerSymbol, double newValue) {
System.out.println("currentValue...");
System.out.println(tickerSymbol + " stock is currently priced at $" + newValue);
}
public static void main(String[] args) {
currentValue("SUNW", 12.35);
valueChanged("SUNW", 10);
}
}
public interface StockWatcher {
final String sunTicker = "SUNW";
final String oracleTicker = "ORCL";
final String ciscoTicker = "CSCO";
// void valueChanged(String tickerSymbol, double newValue);
// void currentValue(String tickerSymbol, double newValue);
}

Uncomment those lines and everything should work fine. I would recommend making them public, though, because after all, it is an interface. Override all the methods in a concrete class.

methods in an interface are implicitly public, no need to declare them as such (at least in the interface, in the implementing class they MUST be declared public).

Hello Mr. Lew,

Your code compiles and runs fine because you are utilizing static methods belonging to the class and not the abstract instance methods of the interface that you must implement in a concrete class. If you uncomment the code in the interface, your code will not compile. The reason, you've added a static modifier to the methods which is not part of the binding contract when you implement the interface. Get rid of the static modifiers and create an instance of the StockApp class. Don't forget to change the way you invoke the methods in the main method. You cannot reference instances from a static context! Oh yeah, constants in java are all CAPS and words are separated by an underscore character ( e.g. [public final static String SOME_TEXT_MR_FEW = ''Future Java Guru'';] )

Edited 3 Years Ago by Dani: Formatting fixed

Thanks to everyone for the help... I finally got the solution, funny how the book pushed me towards a problem but didn't cover the solution until the next chapter... this text book really sucks.

For those who may run into the saem problem, I'm posting my solution.

In order to reference a non-static element from a static one (such as "main") you need to have an object reference available for the non-static element.

http://www.informit.com/articles/article.asp?p=102342&seqNum=2&rl=1

Here is my final code:

public class StockApp implements StockWatcher {
  public void valueChanged(String tickerSymbol, double newValue) {
      if (tickerSymbol.equals(sunTicker)) {
        System.out.println("sunTicker...");
        System.out.println(tickerSymbol + " stock just went up $" + newValue);
     } else if (tickerSymbol.equals(oracleTicker)) {
        System.out.println("oracleTicker...");
        System.out.println(tickerSymbol + " stock just went up $" + newValue);
     } else if (tickerSymbol.equals(ciscoTicker)) {
        System.out.println("ciscoTicker...");
        System.out.println(tickerSymbol + " stock just went up $" + newValue);
     }
  }
  public void currentValue(String tickerSymbol, double newValue) {
     System.out.println("currentValue...");
     System.out.println(tickerSymbol + " stock is currently priced at $" + newValue);
  }
  public static void main(String args[]) {
     StockApp sa = new StockApp();
     sa.currentValue("SUNW", 12.35);
     sa.valueChanged("SUNW", 10);
  }
}
public interface StockWatcher {
 final String sunTicker = "SUNW";
 final String oracleTicker = "ORCL";
 final String ciscoTicker = "CSCO";
 void valueChanged(String tickerSymbol, double newValue);
 void currentValue(String tickerSymbol, double newValue);
}

the book is designed to make you think for yourself rather than present everything to you in nice predigested chunks or cud.

But a teaching book should teach... meaning cover a subject/concept, and then use exercises to reinforce those subjects/concepts. This book doesn't even come close... too much left out, code with incorrect syntax, concepts with faulty implementations... the list goes on. The course instructor actually has moved to a new book just for this reason, I just missed the new material by a few weeks.

The Java certification book gives you all the details about everything including interfaces and many other things. It's a MUST read if you want specifics.

I initially learned Java from O'Reilly's Java in a Nutshell, then went on to read the JLS.
I now have enough books about the platform to fill a small library...

Hi
Your error is you are trying to implement a method of an interface
as static
your code :

interface StockWatcher {
final String sunTicker = "SUNW";
final String oracleTicker = "ORCL";
final String ciscoTicker = "CSCO";
 //void  valueChanged(String tickerSymbol, double newValue);
 //void currentValue(String tickerSymbol, double newValue);
}
 
class StockApp implements StockWatcher {
//You are trying to implement an interface method as static 
public static void valueChanged(String tickerSymbol, double newValue) {
if (tickerSymbol.equals(sunTicker)) {
System.out.println("sunTicker...");
System.out.println(tickerSymbol + " stock just went up $" + newValue);
} else if (tickerSymbol.equals(oracleTicker)) {
System.out.println("oracleTicker...");
System.out.println(tickerSymbol + " stock just went up $" + newValue);
} else if (tickerSymbol.equals(ciscoTicker)) {
System.out.println("ciscoTicker...");
System.out.println(tickerSymbol + " stock just went up $" + newValue);
}
}
//  You are trying to implement an method of an interface as static
public static void currentValue(String tickerSymbol, double newValue) {
System.out.println("currentValue...");
System.out.println(tickerSymbol + " stock is currently priced at $" + newValue);
}
public static void main(String[] args) {
  StockApp a = new StockApp();        
 
 
 a.currentValue("SUNW", 12.35);
a.valueChanged("SUNW", 10);
}
}

My solution is folloing

//if you want to declare the interface StockWatcher as public save it in file name called StockWatcher.java
// the StockWatcher.class file should be in the same package whare your StockApp.class resides
 interface StockWatcher {
final String sunTicker = "SUNW";
final String oracleTicker = "ORCL";
final String ciscoTicker = "CSCO";
void valueChanged(String tickerSymbol, double newValue);
 void currentValue(String tickerSymbol, double newValue);
}
//if you want to declare the class StockApp as public save it in file name called StockApp.java
// the StockApp.class should be in the same package whare your StockWatcher.class resides
class StockApp implements StockWatcher {
public  void valueChanged(String tickerSymbol, double newValue) {
if (tickerSymbol.equals(sunTicker)) {
System.out.println("sunTicker...");
System.out.println(tickerSymbol + " stock just went up $" + newValue);
} else if (tickerSymbol.equals(oracleTicker)) {
System.out.println("oracleTicker...");
System.out.println(tickerSymbol + " stock just went up $" + newValue);
} else if (tickerSymbol.equals(ciscoTicker)) {
System.out.println("ciscoTicker...");
System.out.println(tickerSymbol + " stock just went up $" + newValue);
}
}
public  void currentValue(String tickerSymbol, double newValue) {
System.out.println("currentValue...");
System.out.println(tickerSymbol + " stock is currently priced at $" + newValue);
}
 
}
public class StockImplements {
    public static void main(String[] args) {
  StockApp a = new StockApp();        
 
 
 a.currentValue("SUNW", 12.35);
a.valueChanged("SUNW", 10);
}
}

:lol: :lol: by

This article has been dead for over six months. Start a new discussion instead.