The implements keyword in Java is used by a class to inherit the abstract methods of an interface. This allows a class to provide concrete implementations for all the methods defined in the interface. Using the implements keyword, a class can agree to perform specific behaviors as dictated by the interface.
Table of Contents
- Introduction
implementsKeyword Syntax- Understanding
implements - Examples
- Implementing a Single Interface
- Implementing Multiple Interfaces
- Real-World Use Case
- Conclusion
Introduction
In Java, interfaces define methods that a class must implement. The implements keyword is used by a class to indicate that it will provide implementations for the methods defined by one or more interfaces. This is crucial for ensuring that a class adheres to a particular contract or protocol defined by the interface.
implements Keyword Syntax
The syntax for using the implements keyword is as follows:
class ClassName implements InterfaceName {
// Implement all the methods of InterfaceName
}
Example:
class Car implements Vehicle {
// Implement all the methods of Vehicle interface
}
Understanding implements
When a class uses the implements keyword, it must provide concrete implementations for all the methods declared in the interface. If the class does not implement all the methods, it must be declared abstract.
Key Points:
- A class can implement multiple interfaces, separated by commas.
- The implementing class must provide definitions for all abstract methods of the interface(s).
- Interfaces provide a way to achieve multiple inheritance in Java, as a class can implement multiple interfaces.
Examples
Implementing a Single Interface
Let’s implement a simple interface named Vehicle.
Example
interface Vehicle {
void start();
void stop();
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car is starting.");
}
@Override
public void stop() {
System.out.println("Car is stopping.");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.start();
myCar.stop();
}
}
Output:
Car is starting.
Car is stopping.
Implementing Multiple Interfaces
A class can implement multiple interfaces by separating them with commas.
Example
interface Drivable {
void drive();
}
interface Maintainable {
void service();
}
class Truck implements Drivable, Maintainable {
@Override
public void drive() {
System.out.println("Truck is driving.");
}
@Override
public void service() {
System.out.println("Truck is being serviced.");
}
}
public class Main {
public static void main(String[] args) {
Truck myTruck = new Truck();
myTruck.drive();
myTruck.service();
}
}
Output:
Truck is driving.
Truck is being serviced.
Real-World Use Case
Payment System
In a payment system, you might have different payment methods such as credit card and PayPal. You can define an interface PaymentMethod to ensure that all payment methods implement the same set of methods.
Example
interface PaymentMethod {
void pay(double amount);
}
class CreditCard implements PaymentMethod {
@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using Credit Card.");
}
}
class PayPal implements PaymentMethod {
@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using PayPal.");
}
}
public class Main {
public static void main(String[] args) {
PaymentMethod payment1 = new CreditCard();
payment1.pay(100.0);
PaymentMethod payment2 = new PayPal();
payment2.pay(200.0);
}
}
Output:
Paid 100.0 using Credit Card.
Paid 200.0 using PayPal.
Conclusion
The implements keyword in Java is used by a class to indicate that it will provide concrete implementations for all the methods defined by one or more interfaces. This allows for flexible and modular code design, as well as adherence to specific contracts defined by interfaces. Understanding and using the implements keyword is crucial for working with interfaces and achieving multiple inheritance in Java.