In Java, an interface is a special abstract type that defines a “contract” (i.e., method declarations) for a series of methods but does not provide specific implementations. The implements keyword allows a class to “adhere to” these contracts, thereby enabling the class to possess the capabilities defined in the interface. In simple terms, an interface is like a “contract,” and once a class “signs” it using the implements keyword, it must provide concrete method implementations as required by the contract.
一、What is an Interface?¶
Imagine you need different classes (e.g., Dog, Cat, Bird) to all have the ability to “bark” and “move.” If you directly write the bark() and move() methods in each class, the code will be repetitive and hard to maintain. This is where interfaces come in: you can first define an Animal interface declaring the bark() and move() methods, then have all animal classes implement this interface. This way, each class only needs to focus on “how it barks” and “how it moves,” without repeating method declarations.
Characteristics of Interfaces:
- An interface is defined using the interface keyword. For example: interface Animal { void bark(); void move(); }
- Methods in an interface are abstract by default (no method body), meaning they only have method declarations without specific implementations.
- Interfaces cannot be instantiated (cannot be created with new). They can only be implemented by classes or inherited by other interfaces.
二、Role of the implements Keyword¶
The implements keyword is used in Java to implement interfaces. When a class uses implements to declare that it implements an interface, the class must “fulfill” the promises of all abstract methods in the interface—i.e., provide concrete method implementations.
三、How to Use implements to Implement an Interface?¶
Implementing an interface involves three simple steps:
Step 1: Define the Interface¶
First, define an interface containing the method declarations that need to be implemented. For example, define an Animal interface with bark() and move() methods:
// Define the Animal interface
interface Animal {
// Abstract method: Declares the "bark" behavior without implementation
void bark();
// Abstract method: Declares the "move" behavior without implementation
void move();
}
Step 2: Define a Class and Declare implements¶
In the class declaration, use the implements keyword to specify the interface to be implemented. For example, create a Dog class that implements the Animal interface:
// Dog class implements the Animal interface
class Dog implements Animal {
// All abstract methods in the interface must be implemented here
}
Step 3: Implement All Abstract Methods in the Interface¶
Methods in an interface have no method body, so the implementing class must write concrete code for each abstract method. For example, the Dog class implements bark() and move():
class Dog implements Animal {
// Implement the bark() method from the interface
@Override
public void bark() {
System.out.println("The dog barks!");
}
// Implement the move() method from the interface
@Override
public void move() {
System.out.println("The dog runs on four legs!");
}
}
The @Override annotation tells the compiler that this method is an implementation of a method from the parent interface (or parent class), ensuring the method signature (name, parameters, return type) matches the interface exactly.
四、Example: Giving a Class Interface Capabilities¶
Suppose we need a test class to verify that the Dog class correctly implements the Animal interface:
public class TestAnimal {
public static void main(String[] args) {
// Create a Dog object (using the interface type to reference the implementation class object, demonstrating polymorphism)
Animal dog = new Dog();
dog.bark(); // Call the implemented bark() method
dog.move(); // Call the implemented move() method
}
}
Output:
The dog barks!
The dog runs on four legs!
五、Notes¶
- Must Implement All Abstract Methods: If an interface has multiple abstract methods, the implementing class must implement all of them. Otherwise, the class must be declared
abstract.
interface Bird {
void fly();
void sing();
}
class Sparrow implements Bird {
// Error: Only implementing one method will cause an error "Missing abstract method sing()"
@Override
public void fly() {
System.out.println("Sparrow flaps its wings to fly!");
}
}
-
Method Signatures Must Match: The method in the implementing class must have the exact same name, parameter list, and return type as the method declared in the interface (the return type can be a subclass of the interface method’s return type, i.e., covariant return type).
-
Multiple Interfaces Implemented: Java does not support multiple class inheritance, but it supports a class implementing multiple interfaces (separated by commas). For example:
class Cat implements Animal, Pet {
// Must implement all abstract methods from both Animal and Pet interfaces
}
六、Summary¶
The implements keyword is a core tool in Java for enabling classes to acquire interface capabilities. By defining interfaces to establish specifications, classes “sign” contracts using implements, focusing only on method implementation details to ensure code consistency and maintainability. The essence of an interface is to “define behavior,” while implements is to “endow classes with behavior.” Together, they make Java programs more flexible and extensible.
Remember: An interface specifies “what to do,” implements specifies “how to do it,” and a class, by implementing the interface, transforms the interface’s “contract” into concrete “capabilities.”