Hackforge Academy

Category: java

interface in Java

Published on 04 May 2026

Explanation

An interface in Java is a blueprint of a class that is used to achieve full abstraction. It contains abstract methods (by default) and constants. A class implements an interface using the 'implements' keyword.

Code:

interface Animal {
    void sound();
}

Explanation

Interfaces are used to achieve abstraction by defining methods without implementation, allowing classes to provide their own behavior.

Code:

class Dog implements Animal {
    public void sound() {
        System.out.println("Dog barks");
    }
}

Explanation

They support multiple inheritance in Java, meaning a class can implement multiple interfaces.

Code:

interface A {
    void methodA();
}

interface B {
    void methodB();
}

class Test implements A, B {
    public void methodA() {}
    public void methodB() {}
}

Explanation

Interfaces are used to achieve loose coupling, making code more flexible and maintainable.

Code:

interface Payment {
    void pay();
}

class CreditCard implements Payment {
    public void pay() {
        System.out.println("
Paid using credit card");
    }
}

Explanation

They are widely used in frameworks, APIs, and for implementing design patterns like dependency injection and strategy pattern.

Code:

interface Database {
    void connect();
}

class MySQL implements Database {
    public void connect() {
        System.out.println("
Connected to MySQL");
    }
}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community