Interface Modifiers Tutorial

The following rules apply to a top-level (outer, non-nested) interface:

  • An interface is implicitly abstract.
  • An interface may have only the following legal modifiers: public, abstract, and strictfp. The default (package-private) access modifier is legal as well. Don't worry about strictfp, I haven't covered that one yet.
  • If the public access modifier is applied to an interface, the interface source code filename must match the name of the interface.


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md one
C:\Java>cd one
C:\Java\one>Notepad Tester.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


package one;

import two.*;

class Tester implements InterfaceOne, InterfaceTwo  { 

    public static void main(String args[]) {
        System.out.println(inOne);
        System.out.println(inTwo);
    }
}

interface InterfaceOne {
    public static final String inOne = "Hello from interface ONE!";  // remember all interface variables are public static final implicitly
}

Now switch back to the command prompt (CMD) and type in the following commands.


C:\Java\one>cd ..
C:\Java>md two
C:\Java>cd two
C:\Java\two>notepad InterfaceTwo.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


package two;

public interface InterfaceTwo { 
    String inTwo = "Hello from interface TWO!";
}

Now switch back to the command prompt (CMD) and type in the following commands.


C:\Java\one>cd ..
C:\Java>javac one\Tester.java
C:\Java>java one.Tester 
Hello from interface ONE!
Hello from interface TWO!


Final thoughts

None


Tutorials