JAVA Interfaces and their advantages
JAVA Interfaces and their advantages
JAVA Interfaces
In Java, an interface is a blueprint for a class. It defines a set of static constants and abstract methods that classes implementing the interface must provide.
Interfaces in Java serve as a means to achieve abstraction. They exclusively contain abstract methods without method bodies, facilitating abstraction and enabling multiple inheritance in Java.
In simpler terms, interfaces can contain abstract methods and variables but cannot provide method implementations.
Java interfaces also represent the IS-A relationship, where a class implementing an interface is considered an instance of that interface.
Interfaces cannot be instantiated, similar to abstract classes.
Starting from Java 8, interfaces can include default and static methods, which provide method implementations that can be inherited by implementing classes.
As of Java 9, interfaces can also contain private methods, enhancing their flexibility and maintainability.
Why use Java interface?
highlighted three key reasons to use interfaces in Java:
Achieving Abstraction: Interfaces allow you to define a contract or a set of abstract methods that classes implementing the interface must provide. This abstraction separates the "what" (the interface) from the "how" (the implementation), promoting code modularity and maintainability.
Supporting Multiple Inheritance: Java supports multiple inheritance through interfaces. A class can implement multiple interfaces, inheriting the abstract methods and constants from each. This feature provides flexibility in designing and structuring your classes while avoiding some of the issues associated with traditional multiple inheritance.
Achieving Loose Coupling: Interfaces help achieve loose coupling by decoupling the client code from the specific implementations. Clients depend on the interface, not on the concrete classes that implement it. This reduces dependencies and makes it easier to change or extend the implementation without affecting the client code.
Declared an interface
You declare an interface in Java using the "interface" keyword. Interfaces offer complete abstraction, which means they consist of methods without implementations, and their fields are automatically public, static, and final. When a class implements an interface, it is required to provide concrete implementations for all the methods declared in that interface.
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
Relationship between classes and interfaces
As illustrated in the provided diagram, one class inherits from another class, one interface extends another interface, whereas a class incorporates an interface
// Define the 'printable' interface
interface Printable {
void print();
}
// Implement the 'printable' interface in class 'A6'
class A6 implements Printable {
// Implement the 'print' method required by the interface
public void print() {
System.out.println("Hello");
}
// Entry point of the program
public static void main(String[] args) {
// Create an object of class 'A6'
A6 obj = new A6();
// Call the 'print' method on the object
obj.print();
}
}
Output:-
Hello
Java Interface Example :-
// Define the Drawable interface
interface Drawable {
void draw(); // This method must be implemented by implementing classes
}
// Create a class Circle that implements the Drawable interface
class Circle implements Drawable {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}
}
// Create a class Rectangle that implements the Drawable interface
class Rectangle implements Drawable {
private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public void draw() {
System.out.println("Drawing a rectangle with width " + width + " and height " + height);
}
}
public class Main {
public static void main(String[] args) {
// Create objects of Circle and Rectangle
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(8, 6);
// Draw the objects using the draw() method
circle.draw();
rectangle.draw();
}
}
In this example:
We define the Drawable interface with a single method, draw(), which has no implementation.
We create two classes, Circle and Rectangle, both of which implement the Drawable interface. These classes provide their own implementations of the draw() method.
In the Main class, we create objects of the Circle and Rectangle classes and call the draw() method on these objects. Since both classes implement the Drawable interface, we can treat them polymorphically and call the draw() method without knowing the concrete type of the objects.
When you run the Main class, it will output:
Drawing a circle with radius 5
Drawing a rectangle with width 8 and height 6