hello all
i'm trying to learn about interfaces. my understanding is that an interface forces the class implementing it to call all the method variables defined in it. am i right about the basic idea?
this is a little code i've written just to see if i got it right and i can't compile it for a few reason. i have two .java files: myInterface and myClass:
PHP Code:
// The interface
public interface myInterface {
public void printWord (String word);
}
PHP Code:
//Class implementing it
class myClass implements myInterface {
public static void main (String[] args) {
String word="Hello World";
printWord(word);
}
public void printWord(String word) {
System.out.println(word);
}
}
The error i keep getting:
PHP Code:
myClass.java:5: non-static method printWord(java.lang.String) cannot be referenced from a static context
printWord(word);
^
1 error
so first - how do i make it right.
second - can the interface, class implementing it and the method all be in the same file?
thank you!