Interfaces
Interfaces are abstract types describing methods and variables that should exist in any class that implements the interface. The use of an interface is similar to class inheritance in that the class implementing the interface “inherits” its methods. But, unlike class inheritance, a given class can implement multiple interfaces at once.
When a variable is defined as having the data type of an interface, it means it can hold any instance of a class implementing that interface, the interface itself can not be instantiated directly. Interfaces are classified as a reference data type.
The following items are allowed in an interface definition:
- Constant variables: These are
public
,static
, andfinal
by definition. - Abstract methods: These must be overridden by the class implementing the interface.
- Static methods: These are not overridden but accessed like any class static method.
- Default methods: These may be overridden, but if not, the definition in the interface is used.
Note: No interface method can be protected
or final
.
Syntax
Below is the basic syntax defining an interface:
interface InterfaceName {
String constantVariable = "value";
int abstractMethod();
static void staticMethod() {
// Method body
}
default void defaultMethod() {
// Method body
}
}
The interface can have any number of each of these elements, but it must have at least one. It cannot be empty.
Example
The following example uses a Food
interface to demonstrate polymorphism, the ability of an object to take different forms at runtime:
// Food.javapublic interface Food {String name();default String kind() {return "Food";}}
Next, a Cabbage
class is defined, implementing the Food
interface:
// Cabbage.javapublic class Cabbage implements Food {@Overridepublic String name() {return "Cabbage";}@Overridepublic String kind() {return "Vegetable";}}
Define the Sausage
class implementing Food
:
// Sausage.javapublic class Sausage implements Food {@Overridepublic String name() {return "Sausage";}@Overridepublic String kind() {return "Meat";}}
Multiple classes can implement the same interface, like with the Pizza
class below:
// Pizza.javapublic class Pizza implements Food {@Overridepublic String name() {return "Pizza";}}
The following demonstrates the interface and all the classes implementing it:
// Main.javapublic class Main {public static void main(String[] args) {Collection<Food> foods = new ArrayList<Food>();foods.add(new Cabbage());foods.add(new Sausage());foods.add(new Pizza());for (Food food : foods) {System.out.println(food.name() + ": " + food.kind());}}}
This outputs the following:
Cabbage: VegetableSausage: MeatPizza: Food
All contributors
- Anonymous contributorAnonymous contributor5 total contributions
- Christine_Yang271 total contributions
- StevenSwiniarski474 total contributions
- Anonymous contributor
- Christine_Yang
- StevenSwiniarski
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.