Java Reference
In-Depth Information
How It Works
A Java interface is a construct that is used to define the structures, be it variables or
methods that a class must implement. In most cases, interfaces do not include any
method implementations; rather, they only include method signatures. Interfaces can
include variables that are implicitly static and final .
Note As of Java SE 8, it is possible for interfaces to contain method implementa-
tions. Such methods are known as default methods. See Recipe 5-7 for more details.
In the solution to this recipe, the interface does not include any constant variable
declarations. However, it includes four method signatures. All the method signatures
have no access modifier specified because all declarations within an interface are im-
plicitly public . Interfaces are used to expose a set of functionality; therefore, all
methods exposed within an interface must be implicitly public . Any class that imple-
ments an interface must provide the implementation for any method signatures de-
clared in the interface, with the exception of default methods and abstract classes (see
Recipes 5-7 and 5-13 for more details), in which case an interface may leave the imple-
mentation for one of its subclasses.
While the Java language does not allow multiple inheritance, a Java class can im-
plement multiple interfaces, allowing for a controlled form of multiple inheritance. Ab-
stract classes can also implement interfaces. The following code demonstrates a class
implementing an interface: the Team object declaration implements the TeamType in-
terface.
public class Team implements TeamType {
private List<Player> players;
private String name;
private String city;
/**
* @return the players
*/
public List<Player> getPlayers() {
return players;
}
Search WWH ::




Custom Search