Java includes a concept called interfaces. A Java interface is a bit like a class, except a Java interface
can only contain method signatures and fields. An Java interface cannot contain an implementation of the methods, only
the signature (name, parameters and exceptions) of the method.
You can use interfaces in Java as a way to achieve polymorphism. I will get back to polymorphism later in this text.
Here is a simple Java interface example:
public interface MyInterface { public String hello = "Hello"; public void sayHello(); }
As you can see, an interface is declared using the Java
interface
keyword. Just like
with classes, a Java interface can be declared public
or package scope (no access modifier).
The interface example above contains one variable and one method. The variable can be accessed directly
from the interface, like this:
System.out.println(MyInterface.hello);
As you can see, accessing a variable from an interface is very similar to accessing a static variable in a class.
The method, however, needs to be implemented by some class before you can access it.
The next section will explain how that is done.
Implementing an Interface
Before you can really use an interface, you must implement that interface in some Java class.
Here is a class that implements the
MyInterface
interface shown above: public class MyInterfaceImpl implements MyInterface { public void sayHello() { System.out.println(MyInterface.hello); } }
Notice the
implements MyInterface
part of the above
class declaration. This signals to the Java compiler that the MyInterfaceImpl
class implements the MyInterface
interface.
A class that implements an interface must implement all the methods declared in the interface.
The methods must have the exact same signature (name + parameters) as declared in the interface.
The class does not need to implement (declare) the variables of an interface. Only the methods.
Interface Instances
Once a Java class implements an Java interface you can use an instance of that class as an instance of
that interface. Here is an example:
MyInterface myInterface = new MyInterfaceImpl(); myInterface.sayHello();
Notice how the variable is declared to be of the interface type
MyInterface
while the object created is of type MyInterfaceImpl
. Java allows this because the class
MyInterfaceImpl
implements the MyInterface
interface. You can then
reference instances of the MyInterfaceImpl
class as instances of the MyInterface
interface.
You cannot create instances of a Java interface by itself. You must always create an instance of some class
that implements the interface, and reference that instance as an instance of the interface.
Implementing Multiple Interfaces
A Java class can implement multiple Java interfaces. In that case the class must implement all the
methods declared in all the interfaces implemented. Here is an example:
public class MyInterfaceImpl implements MyInterface, MyOtherInterface { public void sayHello() { System.out.println("Hello"); } public void sayGoodbye() { System.out.println("Goodbye"); } }
This class implements two interfaces called
MyInterface
and MyOtherInterface
.
You list the names of the interfaces to implement after the implements
keyword, separated
by a comma.
If the interfaces are not located in the same packages as the implementing class, you will
also need to import the interfaces. Java interfaces are imported using the
import
instruction just like Java classes. For instance: import com.jenkov.package1.MyInterface; import com.jenkov.package2.MyOtherInterface; public class MyInterfaceImpl implements MyInterface, MyOtherInterface { ... }
Here are the two Java interfaces implemented by the class above:
public interface MyInterface { public void sayHello(); }
public interface MyOtherInterface { public void sayGoodbye(); }
As you can see, each interface contains one method. These methods are implemented by the
class
MyInterfaceImpl
. Interface Variables
A Java interface can contain both variables and constants. However, often it does not makes sense to
place variables in an interface. In some cases it can make sense to define constants in an
interface. Especially if those constants are to be used by the classes implementing the
interface, e.g. in calculations, or as parameters to some of the methods in the interface.
However, my advice to you is to avoid placing variables in Java interfaces if you can.
All variables in an interface are public, even if you leave out the
public
keyword
in the variable declaration.
Interface Methods
A Java interface can contain one or more method declarations. As mentioned earlier, the interface
cannot specify any implementation for these methods. It is up to the classes implementing the interface
to specify an implementation.
All methods in an interface are public, even if you leave out the
public
keyword
in the method declaration.
Interface Static Methods
A Java interface can have static methods. Static methods in a Java interface must have implementation.
Here is an example of a static method in a Java interface:
public interface MyInterface { public static void print(String text){ System.out.print(text); } }
Calling a static method in an interface looks and works just like calling a static method in a class.
Here is an example of calling the static
print()
method from the above MyInterface
interface:
MyInterface.print("Hello static method!");
Static methods in interfaces can be useful when you have some utility methods you would like to make available,
which fit naturally into an interface related to the same responsibility. For instance, a
Vehicle
interface could have a printVehicle(Vehicle v)
static method.
Interfaces and Inheritance
It is possible for a Java interface to inherit from another Java interface, just like classes
can inherit from other classes. You specify inheritance using the
extends
keyword. Here is a simple interface inheritance example: public interface MySuperInterface { public void saiHello(); }
public interface MySubInterface extends MySuperInterface { public void sayGoodbye(); }
The interface
MySubInterface
extends the interface MySuperInterface
.
That means, that the MySubInterface
inherits all field and methods from MySuperInterface
.
That then means, that if a class implements MySubInterface
, that class has to implement
all methods defined in both MySubInterface
and MySuperInterface
.
It is possible to define methods in a subinterface with the same signature (name + parameters) as
methods defined in a superinterface, should you find that desirable in your design, somehow.
Unlike classes, interfaces can actually inherit from multiple superinterfaces. You specify that
by listing the names of all interfaces to inherit from, separated by comma. A class implementing
an interface which inherits from multiple interfaces must implement all methods from the interface
and its superinterfaces.
Here is an example of a Java interface that inherits from multiple interfaces:
public interface MySubInterface extends SuperInterface1, SuperInterface2 { public void sayItAll(); }
As when implementing multiple interfaces, there are no rules for how you handle
the situation when multiple superinterfaces have methods with the same signature (name + parameters).