Fundamentals 10 min read

Java Interface Implementation: A Step‑by‑Step Tutorial from Scratch

This tutorial walks through Java interface basics, showing how to declare an interface, implement it in classes, handle method and constant conflicts when implementing multiple interfaces, and demonstrates the concepts with concrete Rectangle and Circle examples.

Lisa Notes
Lisa Notes
Lisa Notes
Java Interface Implementation: A Step‑by‑Step Tutorial from Scratch

In a collaborative software project, developers agree on contracts called interfaces to ensure consistent interaction. An interface defines a set of methods (and optionally constants) that implementing classes must provide.

1. Interface declaration and implementation syntax

A class implements an interface by using the implements keyword in its declaration:

public class MyClass implements MyInterface { 
    // class body
    // must implement all methods declared in MyInterface
}

2. Example: Compare interface

The Compare interface declares a single method int isCompare(Compare other) that returns 1, 0, or -1 depending on whether the invoking object is larger, equal, or smaller than the argument.

public interface Compare {
    int isCompare(Compare other);
}

A RectanglePlus class implements Compare and provides fields, constructors, a getArea() method, and the required isCompare implementation that compares rectangle areas.

public class RectanglePlus implements Compare {
    public int width = 0, length = 0;
    public Point origin;
    // constructors omitted for brevity
    public int getArea() { return width * length; }
    public int isCompare(Compare other) {
        RectanglePlus otherRect = (RectanglePlus) other;
        if (this.getArea() < otherRect.getArea()) return -1;
        if (this.getArea() > otherRect.getArea()) return 1;
        return 0;
    }
}

A test program creates two RectanglePlus objects, calls isCompare, and prints whether the first rectangle is smaller, larger, or equal to the second.

public class RectanglePlusTest {
    public static void main(String[] args) {
        RectanglePlus r1 = new RectanglePlus(6, 3);
        RectanglePlus r2 = new RectanglePlus(4, 8);
        int result = r1.isCompare(r2);
        switch (result) {
            case -1: System.out.println("Rectangle r1 is smaller than r2."); break;
            case 1:  System.out.println("Rectangle r1 is larger than r2."); break;
            default: System.out.println("Rectangle r1 and r2 are equal.");
        }
    }
}

3. Handling multiple‑interface conflicts

When a class implements two interfaces that declare the same constant or method, the class must resolve the conflict:

If methods clash, the class implements a single method that satisfies both interfaces.

If constants clash, the class references the desired constant with the fully‑qualified name InterfaceName.CONSTANT.

The article defines two interfaces, Circle and CirclePlus, each declaring a constant PAI (π) and a getArea() method. CirclePlus also declares a draw() method.

public interface Circle {
    float PAI = 3.14f;
    float getArea();
    float getGirth();
}

public interface CirclePlus {
    float PAI = 3.14159f;
    float getArea();
    void draw();
}

The DrawGraph class implements both interfaces. It provides a single getArea() implementation that uses Circle.PAI to avoid constant ambiguity, and implements getGirth() using CirclePlus.PAI. The draw() method simply prints a message.

public class DrawGraph implements Circle, CirclePlus {
    private float radius;
    public DrawGraph(float r) { this.radius = r; }
    @Override
    public float getArea() {
        return Circle.PAI * radius * radius;
    }
    @Override
    public float getGirth() {
        return 2 * CirclePlus.PAI * radius;
    }
    @Override
    public void draw() {
        System.out.println("Drawing a circle here!");
    }
}

A test program creates a DrawGraph instance, prints the computed area and circumference, and calls draw():

public class DrawGraphTest {
    public static void main(String[] args) {
        DrawGraph dg = new DrawGraph(5);
        System.out.println("Area: " + dg.getArea());
        System.out.println("Circumference: " + dg.getGirth());
        dg.draw();
    }
}

Running the program produces:

Area: 78.5
Circumference: 31.415901
Drawing a circle here!

The example demonstrates how to resolve constant and method name conflicts by qualifying constants with the interface name and implementing a shared method only once.

Overall, the article teaches the mechanics of Java interfaces, the implements keyword, concrete coding patterns for single and multiple interface implementation, and best practices for avoiding naming collisions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javacode exampleOOPInterfaceimplementsmultiple interfaces
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.