Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Mastering Java Interfaces: Concepts, Implementation, and Best Practices

Java Interfaces Explained

Java interfaces define a contract that implementing classes must honor. They enable abstraction, multiple inheritance, and promote a clean, modular architecture.

Defining an Interface

An interface is a fully abstract type. All its methods are implicitly public abstract, and all fields are public static final. It is declared with the interface keyword:

interface Language {
  void getType();
  void getVersion();
}

Here, Language is an interface containing two abstract methods.

Implementing an Interface

Interfaces cannot be instantiated. Classes implement them using the implements keyword and provide concrete bodies for all abstract methods.

Example 1: Polygon Interface

interface Polygon {
  void getArea(int length, int breadth);
}

class Rectangle implements Polygon {
  @Override
  public void getArea(int length, int breadth) {
    System.out.println("The area of the rectangle is " + (length * breadth));
  }
}

class Main {
  public static void main(String[] args) {
    Rectangle r = new Rectangle();
    r.getArea(5, 6);
  }
}

Output:

The area of the rectangle is 30

Example 2: Language Interface

interface Language {
  void getName(String name);
}

class ProgrammingLanguage implements Language {
  @Override
  public void getName(String name) {
    System.out.println("Programming Language: " + name);
  }
}

class Main {
  public static void main(String[] args) {
    ProgrammingLanguage lang = new ProgrammingLanguage();
    lang.getName("Java");
  }
}

Output:

Programming Language: Java

Implementing Multiple Interfaces

A class may implement several interfaces:

interface A { /* members */ }
interface B { /* members */ }
class C implements A, B { /* implementations */ }

Extending an Interface

Interfaces can extend other interfaces using extends:

interface Line { /* members */ }
interface Polygon extends Line { /* members */ }

A class that implements Polygon must provide methods for both Line and Polygon members.

Extending Multiple Interfaces

interface A { /* ... */ }
interface B { /* ... */ }
interface C extends A, B { /* ... */ }

Why Use Interfaces?

Example of implicit constants:

interface Language {
  String TYPE = "programming language"; // implicitly public static final
  void getName(); // implicitly public
}

Default Methods (Java 8+)

Java 8 introduced default methods, allowing interfaces to contain concrete method bodies:

default void getSides() {
  System.out.println("I can get sides of a polygon.");
}

Default methods enable backward compatibility when adding new methods to interfaces without breaking existing implementations.

Example: Default Method Usage

interface Polygon {
  void getArea();
  default void getSides() {
    System.out.println("I can get sides of a polygon.");
  }
}

class Rectangle implements Polygon {
  @Override
  public void getArea() {
    int length = 6;
    int breadth = 5;
    int area = length * breadth;
    System.out.println("The area of the rectangle is " + area);
  }
  @Override
  public void getSides() {
    System.out.println("I have 4 sides.");
  }
}

class Square implements Polygon {
  @Override
  public void getArea() {
    int length = 5;
    int area = length * length;
    System.out.println("The area of the square is " + area);
  }
}

class Main {
  public static void main(String[] args) {
    Rectangle r = new Rectangle();
    r.getArea();
    r.getSides();
    Square s = new Square();
    s.getArea();
    s.getSides();
  }
}

Output:

The area of the rectangle is 30
I have 4 sides.
The area of the square is 25
I can get sides of a polygon.

Static and Private Methods (Java 9+)

Interfaces can now contain static methods, callable via the interface name:

interface Polygon {
  static void helper() { /* ... */ }
}

Polygon.helper();

Private methods provide internal utilities for other interface methods, ensuring encapsulation.

Practical Example: Polygon Calculations

import java.lang.Math;

interface Polygon {
  void getArea();
  default void getPerimeter(int... sides) {
    int perimeter = 0;
    for (int side : sides) perimeter += side;
    System.out.println("Perimeter: " + perimeter);
  }
}

class Triangle implements Polygon {
  private final int a, b, c;
  private double area;

  Triangle(int a, int b, int c) {
    this.a = a; this.b = b; this.c = c;
  }

  @Override
  public void getArea() {
    double s = (a + b + c) / 2.0;
    area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
    System.out.println("Area: " + area);
  }
}

class Main {
  public static void main(String[] args) {
    Triangle t = new Triangle(2, 3, 4);
    t.getArea();
    t.getPerimeter(2, 3, 4);
  }
}

Output:

Area: 2.9047375096555625
Perimeter: 9

In this example, the Polygon interface supplies a reusable getPerimeter method, while each shape implements its own area calculation.

Java

  1. Java Collections Framework: Core Interfaces, Implementations, and Practical Usage
  2. Java Collection Interface: Core Concepts & Essential Methods
  3. Mastering Java’s Queue Interface: Methods, Implementations, and Practical Use
  4. Mastering Java's Deque Interface: Features, Methods, and Practical Examples
  5. Java Map Interface – Comprehensive Guide to Map, Its Implementations, and Key Methods
  6. Java SortedMap Interface: Overview, Methods, and TreeMap Implementation
  7. Mastering Java NavigableMap: Features, Methods, and TreeMap Implementation
  8. Mastering Java’s ConcurrentMap: Thread‑Safe Maps Explained
  9. Mastering Java’s Set Interface: Concepts, Methods, and Practical Examples
  10. Mastering Java SortedSet: A Practical Guide to TreeSet and Its Methods