The interface
keyword in Java is used to declare an interface. An interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields or constructors. They are a way to achieve abstraction and multiple inheritance in Java.
Table of Contents
- Introduction
interface
Keyword Syntax- Understanding Interfaces
- Examples
- Basic Interface Declaration
- Implementing an Interface
- Default Methods
- Static Methods
- Real-World Use Case
- Conclusion
Introduction
An interface in Java is a blueprint of a class. It has static constants and abstract methods. Interfaces are used to achieve abstraction and provide a way to implement multiple inheritance in Java. They allow a class to specify methods that it must implement, providing a form of contract between the interface and the implementing class.
interface Keyword Syntax
The syntax for declaring an interface is as follows:
interface InterfaceName { // constant fields // abstract methods // default methods // static methods }
Example:
interface Vehicle { void start(); void stop(); }
Understanding Interfaces
An interface can contain abstract methods (methods without a body) that need to be implemented by classes that implement the interface. It can also contain default methods (methods with a body) and static methods.
Key Points:
- Interfaces cannot be instantiated.
- A class can implement multiple interfaces.
- Interfaces can contain abstract methods, default methods, and static methods.
- Fields in interfaces are implicitly public, static, and final.
Examples
Basic Interface Declaration
Let’s declare a simple interface named Vehicle
.
Example
interface Vehicle { void start(); void stop(); }
Implementing an Interface
A class implements an interface by using the implements
keyword and providing the method definitions for all the methods declared in the interface.
Example
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:
Compilation failed.
Default Methods
Interfaces can have default methods, which are methods with a body. These methods are not required to be implemented by classes that implement the interface.
Example
interface Vehicle { void start(); void stop(); default void honk() { System.out.println("Honking the horn."); } } 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.honk(); myCar.stop(); } }
Output:
Car is starting. Honking the horn. Car is stopping.
Static Methods
Interfaces can also have static methods, which are called on the interface itself, not on instances of the implementing class.
Example
interface Vehicle { void start(); void stop(); static void service() { System.out.println("Servicing the vehicle."); } } public class Main { public static void main(String[] args) { Vehicle.service(); } }
Output:
Servicing the vehicle.
Real-World Use Case
Payment System
In a payment system, you might have different payment methods such as credit card, debit 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 interface
keyword in Java is a powerful feature that allows you to define abstract methods that must be implemented by classes. It provides a way to achieve abstraction and multiple inheritance. By understanding and using interfaces, you can create flexible and maintainable Java programs.