Java implements Keyword:
Java Implements Keyword:
In Java programming, the implements keyword plays a crucial role in achieving one of the fundamental principles of object-oriented programming (OOP) — interfaces. This keyword enables classes to implement interfaces, thereby fulfilling contracts and facilitating polymorphism within Java applications.
What is an Interface?
An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures (abstract methods), default methods, static methods, and nested types. It defines a blueprint of methods that a class must implement. Unlike classes, interfaces cannot have instance fields or constructors.
Benefits of Using implements
Code Reusability: Interfaces promote code reusability by allowing unrelated classes to implement common behaviors defined by interfaces without inheriting a common superclass.
Polymorphism: Interface-based programming facilitates polymorphism, where objects can be treated as instances of their interface types, enabling flexibility and extensibility in Java applications.
Design by Contract: Interfaces define contracts that classes must adhere to, promoting a structured approach to software design and ensuring consistency in class behaviors.
Using the implements Keyword
The implements keyword is used to establish a relationship between a class and an interface. When a class implements an interface, it agrees to provide concrete implementations for all the abstract methods defined by that interface.
public interface Printable {
void print();
}
public class Printer implements Printable {
@Override
public void print() {
System.out.println("Printing...");
}
}
In this example, the Printer class implements the Printable interface by providing a concrete implementation for the print() method defined in the Printable interface.
Implementing Multiple Interfaces
A Java class can implement multiple interfaces. This allows the class to inherit behaviors from multiple sources and supports a higher level of abstraction and flexibility in code design.
public interface Displayable {
void display();
}
public class Monitor implements Printable, Displayable {
@Override
public void print() {
System.out.println("Printing...");
}
@Override
public void display() {
System.out.println("Displaying...");
}
}
Interface Inheritance
Interfaces can extend other interfaces, creating a hierarchy of interface types. When a class implements an interface that extends another interface, it must provide implementations for all methods defined in both interfaces.
public interface Shape {
void draw();
}
public interface Circle extends Shape {
double getRadius();
}
public class MyCircle implements Circle {
@Override
public void draw() {
System.out.println("Drawing Circle...");
}
@Override
public double getRadius() {
return 5.0;
}
}
Default and Static Methods:
Starting from Java 8, interfaces can have default and static methods with implementations. Classes implementing such interfaces can optionally override default methods or directly call static methods.
public interface Loggable {
default void log(String message) {
System.out.println("Logging: " + message);
}
static void printInfo() {
System.out.println("Log information");
}
}
public class Logger implements Loggable {
// Implements log() method from Loggable interface
}