implements Keyword in Java
The implements
keyword in Java is used to indicate that a class is going to implement an interface. By using implements
, a class agrees to provide concrete implementations for all the abstract methods declared in the interface.
Usage
The implements
keyword is used in a class declaration to specify one or more interfaces that the class will implement.
Syntax
class ClassName implements InterfaceName {
// Class body with implementations of interface methods
}
ClassName
: The name of the class that is implementing the interface.InterfaceName
: The name of the interface being implemented. Multiple interfaces can be implemented by separating them with commas.
Examples
Example 1: Single Interface Implementation
interface Animal {
void makeSound();
}
public class Dog implements Animal {
public void makeSound() {
System.out.println("Bark");
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Outputs: Bark
}
}
In this example, the Dog
class implements the Animal
interface. The makeSound
method is defined in the Dog
class to provide the specific behavior for a dog.
Example 2: Multiple Interface Implementation
interface Animal {
void makeSound();
}
interface Pet {
void play();
}
public class Cat implements Animal, Pet {
public void makeSound() {
System.out.println("Meow");
}
public void play() {
System.out.println("Playing with a ball of yarn");
}
public static void main(String[] args) {
Cat cat = new Cat();
cat.makeSound(); // Outputs: Meow
cat.play(); // Outputs: Playing with a ball of yarn
}
}
Here, the Cat
class implements two interfaces, Animal
and Pet
. It provides concrete implementations for the methods makeSound
and play
.
Example 3: Implementing Interface with Default Methods
interface Vehicle {
void start();
default void stop() {
System.out.println("Vehicle stopped");
}
}
public class Car implements Vehicle {
public void start() {
System.out.println("Car started");
}
public static void main(String[] args) {
Car car = new Car();
car.start(); // Outputs: Car started
car.stop(); // Outputs: Vehicle stopped
}
}
In this example, the Vehicle
interface has a default method stop
. The Car
class implements the Vehicle
interface and provides an implementation for the start
method. The default stop
method is inherited by the Car
class.
Tips and Best Practices
- Interface Segregation: Implement interfaces that are specific to the class's functionality. This adheres to the Interface Segregation Principle (ISP), making the code more modular and easier to maintain.
- Multiple Inheritance: Use interfaces to achieve multiple inheritance in Java. A class can implement multiple interfaces, allowing it to inherit behavior from multiple sources.
- Default Methods: Leverage default methods in interfaces to provide common functionality without forcing all implementing classes to override them.
- Documentation: Document the purpose of each interface and the methods it declares. This helps other developers understand the contract that implementing classes must fulfill.
- Consistency: Ensure consistent naming conventions and method signatures across interfaces and their implementations for clarity and maintainability.
interface Drivable {
void drive();
}
interface Fillable {
void fillFuel();
}
public class Truck implements Drivable, Fillable {
public void drive() {
System.out.println("Truck is driving");
}
public void fillFuel() {
System.out.println("Filling truck with fuel");
}
}