Implement Interface using Abstract Class in Java
What is interface in java?
In java, an interface is a group of abstract methods that is use to form a contract or a set of method so that every class that implements the interface must implement. It offers a means of defining a collection of associated methods that may be accessed by several unconnected classes.
The Java "interface" keyword is used to declare an interface. Interface does not coati any method implementation it is use to only contain method signature and constants. Instead, it is up to the classes that implement the interface to implement the methods.
Lets take an example:
public interface Shape { public double getArea(); public double getPerimeter(); }
As you can all see in the above example we have getArea and getPerimeter that is used to refer to two methods on the shape interface. Both of these methods must be implemented by any class that conforms to this interface.
A class must use the "implements" keyword and give an implementation for each of the interface's methods in order to implement an interface.
For example:
public class Circle implements Shape { private double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } public double getPerimeter() { return 2 * Math.PI * radius; } }
Implementation of interface using abstract class
Let’s take an program to understand how we implement interface using abstract class in java:
// Step 1: Declare the interface interface Shape { double calculateArea(); double calculatePerimeter(); } // Step 2: Create an abstract class implementing the interface abstract class AbstractShape implements Shape { // Optional: Provide default implementations for some methods public double calculatePerimeter() { return 0.0; } // This method will be left abstract, so subclasses must provide an implementation public abstract double calculateArea(); } // Step 3: Create concrete subclasses that extend the abstract class class Circle extends AbstractShape { private double radius; public Circle(double radius) { this.radius = radius; } public double calculateArea() { return Math.PI * radius * radius; } public double calculatePerimeter() { return 2 * Math.PI * radius; } } class Rectangle extends AbstractShape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double calculateArea() { return length * width; } public double calculatePerimeter() { return 2 * (length + width); } } // Step 4: Use the concrete subclasses public class Main { public static void main(String[] args) { Circle circle = new Circle(7.0); System.out.println("Circle Area: " + circle.calculateArea()); System.out.println("Circle Perimeter: " + circle.calculatePerimeter()); Rectangle rectangle = new Rectangle(4.0, 6.0); System.out.println("Rectangle Area: " + rectangle.calculateArea()); System.out.println("Rectangle Perimeter: " + rectangle.calculatePerimeter()); } }
Output:
Circle Area: 153.93804002589985 Circle Perimeter: 43.982297150257104 Rectangle Area: 24.0 Rectangle Perimeter: 20.0
Limitation of interface
Java interfaces contain various restrictions that programmers should be aware of. The following are some interfaces' drawbacks:
- No implementation: Java interfaces can only provide method signatures; they cannot specify how they should be implemented. This implies that any class that implements the interface must offer a unique implementation of each of the interface's methods.
- Cannot contain fields: Except for public static final constants, interfaces are unable to contain fields. This can restrict the functionality of interfaces by preventing them from having instance variables.
- Cannot be instantiated: Interfaces cannot be directly instantiated, which prevents them from being utilised as stand-alone objects. Instead, a class must implement them, which can complicate the coding.
- Cannot change after compilation: After an interface has been compiled, it cannot be updated to modify its methods or signatures. As a result, it may be challenging to alter an interface over time and may cause compatibility problems.
- Can lead to code duplication: Code duplication may result from the necessity for many classes to independently implement methods with similar signatures across various interfaces.
The inability to contain implementation or fields, the inability to be instantiated, the impossibility to alter after compilation, and the potential for code duplication are a few restrictions placed on interfaces in Java. When planning the architecture of their product, developers should be aware of these constraints.