Yes. An interface lists some methods that are expected in a class, but it may have other ones.
A class can implement several interfaces like this:
class SomeClass extends SomeParent implements interfaceA, interfaceB, interfaceC { }
Now the class must implement or inherit the methods listed in each interface.
Here is an example interface definition. The constants don't have to be separated from the methods (as here), but doing so makes the interface easier to read.
interface MyInterface { public static final int CONSTANT = 32; // a constant public static final double PI = 3.14159; // a constant public void methodA( int x ); // a method header public double methodB(); // a method header }
A method in an interface is public
by default.
The constants in an interface are public static final
by default.
Recall that final
means that the value cannot change as the program is running.
Making use of the defaults, the above interface is equivalent to the following:
interface MyInterface { int CONSTANT = 32; // a constant (public static final, by default) double PI = 3.14159; // a constant (public static final, by default) void methodA( int x ); // a method header (public, by default) double methodB(); // a method header (public, by default) }
The second interface (above) is the preferred way to define an interface. The defaults are assumed and not explicitly coded.
public
in the class.
Of course, if you truely need to use pi in a program,
use Math.PI
(discussed in chapter 18.)
interface SomeInterface { public final int X = 32; public double y; public double addup( ); }
Inspect the interface. Is it correct? Click here for a