Interface
Java Interface Overview:
An interface in Java is a fully abstract class that contains abstract methods. Interfaces are created using the
interfacekeyword and cannot be instantiated. They provide a set of rules (abstract methods) that implementing classes must follow.interface Language { public void getType(); public void getVersion(); }Here,
Languageis an interface with abstract methodsgetType()andgetVersion().Implementing an Interface:
Like abstract classes, interfaces cannot be directly instantiated. Other classes must implement the interface using the
implementskeyword. An example demonstrates the implementation of an interface namedPolygon.interface Polygon { void getArea(int length, int breadth); } class Rectangle implements Polygon { public void getArea(int length, int breadth) { System.out.println("The area of the rectangle is " + (length * breadth)); } }Output:
The area of the rectangle is 30Implementing Multiple Interfaces:
Java allows a class to implement multiple interfaces, providing flexibility in design.
Extending an Interface:
Interfaces can extend other interfaces using the
extendskeyword, creating a hierarchy of interfaces.interface Line { // members of Line interface } // Extending interface interface Polygon extends Line { // members of Polygon interface }.Default Methods in Interfaces:
Introduced in Java 8, default methods allow the addition of methods with implementation inside an interface. These methods are inherited like ordinary methods.
interface Polygon { void getArea(); // Default method default void getSides() { System.out.println("I can get sides of a polygon."); } }Default methods help avoid issues when adding new methods to existing interfaces, as implementing classes automatically inherit the new method.
Private and Static Methods in Interface:
Java 8 introduced static methods inside an interface. With Java 9, private methods are also supported. Static methods can be accessed using the interface reference.
interface Polygon { static void staticMethod() { /* implementation */ } } // Accessing static method Polygon.staticMethod();Practical Example of Interface:
A practical example illustrates the use of an interface named
Polygonwith a default method for calculating perimeter and an abstract method for calculating the area. TheTriangleclass implements thePolygoninterface.interface Polygon { void getArea(); // Default method for calculating perimeter default void getPerimeter(int... sides) { int perimeter = 0; for (int side: sides) { perimeter += side; } System.out.println("Perimeter: " + perimeter); } } class Triangle implements Polygon { // Implementation of abstract method for calculating area public void getArea() { // ... calculation logic ... System.out.println("Area: " + area); } }Output:
Area: 2.9047375096555625 Perimeter: 9In this example,
Polygonis an interface with a default method for perimeter calculation, andTriangleprovides an implementation for the area calculation.
Last updated
Was this helpful?