Java Interface

Interfaces in Java are a fundamental part of object-oriented programming that help achieve abstraction and multiple inheritance. This article will explain what an interface is, how it works, and why it’s important. We’ll also explore its key features, advantages, and practical use cases to give you a clear understanding of interfaces in Java.

Contents:

  1. What is an Interface in Java?
  2. Creating and Implementing an Interface in Java
  3. Difference Between Class and Interface in Java
  4. Multiple Interfaces in Java
  5. Types of interfaces in Java
  6. Extending Interfaces in Java
  7. Advantages of Interfaces in Java
  8. Disadvantages of Interfaces in Java
  9. FAQs on Data Types in Java

What is an Interface in Java?

An Interface in Java is like a blueprint for a class. It defines a set of methods that a class must implement, but it doesn’t provide the actual code for those methods. Think of it as a contract—any class that agrees to follow the contract (i.e., implements the interface) has to provide its own versions of the methods defined in the interface.

Creating and Implementing an Interface in Java

In Java, an interface is a way to define a set of methods that a class must implement. It helps in achieving abstraction and multiple inheritance. Let’s go step by step on how to define and implement an interface in Java.

advertisement

Defining an Interface

An interface is created using the interface keyword. It contains method signatures (without implementation) that the implementing class must define.

Example:

// Defining an interface
interface Quiz
{
    // Abstract method (no body)
    void startQuiz(); 
}

Here, Quiz is an interface with a method startQuiz(). Any class that implements Quiz must provide a body for this method.

⚡ Claim Your Free Java Certification - January 2026

Implementing an Interface

A class uses the implements keyword to implement an interface. It must provide implementations for all the interface methods.

Example:

// Implementing the interface in a class
class JavaQuiz implements Quiz
{
    public void startQuiz()
    { 
        // Providing method implementation
        System.out.println("Java Quiz Started!");
    }
}

Now, JavaQuiz implements Quiz and provides the actual definition of startQuiz()

advertisement

Using the Interface in the Main Method

You can’t create objects of an interface, but you can use it as a reference type.

Example:

// Defining an interface
interface Quiz
{
    void startQuiz();
}
 
// Implementing the interface
class JavaQuiz implements Quiz
{
    public void startQuiz()
    {
        System.out.println("Java Quiz Started!");
    }
}
 
public class InterfaceExample
{
    public static void main(String[] args)
    {
        // Using an interface reference
        Quiz quiz = new JavaQuiz();
        // Calls the implemented method
        quiz.startQuiz(); 
    }
}

Output:

Java Quiz Started!

Explanation:

  • The Quiz interface defines the startQuiz() method but doesn’t provide any implementation.
  • The JavaQuiz class implements Quiz and gives startQuiz() an actual definition, printing “Java Quiz Started!”.
  • In the main() method, a Quiz reference (quiz) is created and assigned an instance of JavaQuiz.
  • Calling quiz.startQuiz(); triggers the overridden method inside JavaQuiz.
  • Even though quiz is of type Quiz, it holds a JavaQuiz object, and Java dynamically calls the correct method at runtime—this is runtime polymorphism in action.

Difference Between Class and Interface in Java

The differences between a class and an interface are mentioned below:

Feature Class Interface
Definition A blueprint for creating objects with fields and methods. A contract that defines abstract methods without implementation.
Methods Can have both implemented and abstract methods. Only abstract methods (before Java 8), can have default and static methods (Java 8+).
Variables Can have instance and static variables. Only public, static, and final (constants).
Access Modifiers Supports private, protected, and public members. Methods are public by default, no private/protected methods (until Java 9).
Multiple Inheritance Not supported (except via interfaces). Supports multiple inheritance.
Object Creation Can create objects using new. Cannot create objects, only implemented by classes.
Use Case Used when an entity has both state (variables) and behavior (methods). Used to define a common behavior that multiple classes can implement.

Multiple Interfaces in Java

When a class implements multiple interfaces, it must provide implementations for all the methods defined in those interfaces. This allows a class to inherit behavior from multiple sources, overcoming the limitation of single inheritance in Java.

// Interface for Internship Application
interface Application
{
    void applyForInternship(String name, String domain);
}
 
// Interface for Task Assignment
interface TaskAssignment
{
    void assignTask(String domain);
}
 
// Interface for Certification
interface InternshipCertificate
{
    void issueCertificate(String name, String domain);
}
 
// Class implementing all interfaces
class SanfoundryInternship implements Application, TaskAssignment, InternshipCertificate
{
    private String internName;
    private String domainName;
 
    // Apply for an internship
    public void applyForInternship(String name, String domain)
    {
        this.internName = name;
        this.domainName = domain;
        System.out.println(name + " has applied for the " + domain + " internship at Sanfoundry.");
    }
 
    // Assign task to the intern
    public void assignTask(String domain)
    {
        System.out.println("Assigning " + domain + " related tasks to " + internName + "...");
    }
 
    // Issue certificate after internship completion
    public void issueCertificate(String name, String domain)
    {
        System.out.println("Congratulations " + name + "! You have successfully completed the "
                           + domain + " internship at Sanfoundry.");
    }
}
 
// Main class
public class SanfoundryInternshipSystem
{
    public static void main(String[] args)
    {
        SanfoundryInternship internship = new SanfoundryInternship();
        internship.applyForInternship("Vikas", "Java Development");
        internship.assignTask("Java Development");
        internship.issueCertificate("Vikas", "Java Development");
    }
}

Output:

Vikas has applied for the Java Development internship at Sanfoundry.
Assigning Java Development related tasks to Vikas...
Congratulations Vikas! You have successfully completed the Java Development internship at Sanfoundry.

Explanation:

  • Application, TaskAssignment, and InternshipCertificate interfaces define different stages of the internship.
  • SanfoundryInternship implements all interfaces, managing the internship process.
  • The workflow simulates applying, task assignment, and receiving a certificate after internship completion.

Types of interfaces in Java

Types of interfaces in Java are mentioned below:

  • Marker Interface
  • Functional Interface
  • Normal Interface
  • Nested Interface

1. Marker Interface
An interface with no methods or fields, used to mark a class for special behavior. Examples include Serializable and Cloneable.

import java.io.Serializable;
 
// Marker interface - Serializable
class SanfoundryQuiz implements Serializable
{
    int id;
    String subject;
 
    SanfoundryQuiz(int id, String subject)
    {
        this.id = id;
        this.subject = subject;
    }
}

2. Functional Interface
An interface with only one abstract method. It can also contain default and static methods and supports lambda expressions.

@FunctionalInterface
interface SanfoundryQuiz
{
    void attemptQuiz(String subject);
}
 
public class FunctionalExample
{
    public static void main(String[] args)
    {
        SanfoundryQuiz quiz = (subject) -> System.out.println("Attempting " + subject + " quiz...");
        quiz.attemptQuiz("Java");
    }
}

3. Abstract Interface
An interface that contains multiple abstract methods and is implemented by a class to provide functionality. Examples include List, Set, and Map.

interface SanfoundryCourse
{
    void enroll(String course);
    void startQuiz(String course);
}
 
// Class implementing interface
class JavaCourse implements SanfoundryCourse
{
    public void enroll(String course)
    {
        System.out.println("Enrolled in " + course + " course.");
    }
 
    public void startQuiz(String course)
    {
        System.out.println("Starting " + course + " quiz...");
    }
}

Output:

Enrolled in Java course.
Starting Java quiz...

4. Nested Interface
An interface declared inside another interface or class, used to group related functionality. Examples include Map.Entry and OuterInterface.InnerInterface.

class SanfoundryPlatform
{
    interface Quiz
    {
        void startQuiz(String subject);
    }
}
 
// Implementing nested interface
class JavaQuiz implements SanfoundryPlatform.Quiz
{
    public void startQuiz(String subject)
    {
        System.out.println("Starting " + subject + " quiz on Sanfoundry...");
    }
}

Output:

Starting Java quiz on Sanfoundry...

Extending Interfaces in Java

In Java, an interface can extend another interface using the extends keyword. This allows one interface to inherit the methods of another, promoting code reusability and consistency.

// Parent Interface
interface Learning
{
    void accessCourse(String course);
}
 
// Child Interface extending parent
interface Exam extends Learning
{
    void takeExam(String course);
}
 
// Class implementing child interface
class SanfoundryUser implements Exam
{
    public void accessCourse(String course)
    {
        System.out.println("Accessing " + course + " course on Sanfoundry.");
    }
 
    public void takeExam(String course)
    {
        System.out.println("Taking " + course + " exam on Sanfoundry.");
    }
}
 
// Main class
public class InterfaceExtensionExample
{
    public static void main(String[] args)
    {
        SanfoundryUser user = new SanfoundryUser();
        user.accessCourse("Python Programming");
        user.takeExam("Python Programming");
    }
}

Output:

Accessing Python Programming course on Sanfoundry.
Taking Python Programming exam on Sanfoundry.

Explanation:

  • The Learning interface declares the method accessCourse(String course).
  • The Exam interface extends Learning and adds another method, takeExam(String course).
  • Since Exam inherits Learning, any class implementing Exam must implement both methods.
  • The SanfoundryUser class implements Exam and provides definitions for both methods.
  • In main(), calling accessCourse(“Python Programming”) prints “Accessing Python Programming course on Sanfoundry.”
  • Calling takeExam(“Python Programming”) prints “Taking Python Programming exam on Sanfoundry.”

Advantages of Interfaces in Java

  • Multiple Inheritance Support: Java doesn’t allow multiple inheritance with classes, but interfaces solve this by letting a class implement multiple interfaces.
  • Loose Coupling: Interfaces separate what a class does from how it does it, making code more flexible and easier to modify.
  • Improved Testability: Since interfaces define behavior without implementation, it’s easier to write unit tests and swap implementations when needed.
  • Encourages Code Reusability: Different classes can implement the same interface, reducing code duplication and promoting consistency.
  • Better Maintainability: Changes to an interface don’t impact existing implementations unless new methods are added, keeping the code stable.

Disadvantages of Interfaces in Java

  • No Method Implementation: Interfaces can’t have method bodies (except default and static methods), so every implementing class must define all methods, leading to code duplication.
  • Difficult to Maintain: If an interface is modified by adding new methods, all implementing classes must be updated, which can be time-consuming.
  • Performance Overhead: Calling methods through an interface requires dynamic method resolution at runtime, which is slightly slower than direct method calls in a class.
  • Lack of Constructor Support: Unlike classes, interfaces can’t have constructors, making it impossible to initialize common properties directly.
  • Complexity in Large Projects: Too many interfaces can make the codebase harder to understand and manage, especially if multiple interfaces are interconnected.

FAQs on Java Interface

1. What is an interface in Java?
An interface in Java is a blueprint for a class that defines method signatures but doesn’t provide implementations. Classes implementing an interface must define its methods.

2. How is an interface different from an abstract class?
An abstract class can have both implemented and unimplemented methods, whereas an interface only has method declarations (except for default and static methods). Also, a class can implement multiple interfaces but extend only one abstract class.

3. Can an interface have variables?
Yes, but all variables in an interface are implicitly public, static, and final, meaning they act as constants.

4. What are marker interfaces in Java?
Marker interfaces are empty interfaces (with no methods or fields) used to indicate a special behavior to the JVM, like Serializable and Cloneable.

5. Why are interfaces useful in Java?
Interfaces help achieve abstraction, multiple inheritance, and loose coupling, making code more modular, reusable, and easier to maintain.

6. What happens if a class doesn’t implement all methods of an interface?
The class must be declared abstract, or it will result in a compilation error.

7. Can an interface extend another interface?
Yes, an interface can extend one or more interfaces, inheriting their methods without implementation.

8. Can an interface have variables?
Yes, but all variables in an interface are implicitly public, static, and final, meaning they act as constants.

Key Points to Remember

Here is the list of key points we need to remember about the “Java Interface”.

  • An interface in Java is a blueprint that defines abstract methods but doesn’t provide implementations. It helps achieve abstraction and multiple inheritance.
  • A class implements an interface using the implements keyword and must define all its methods. Interfaces cannot be instantiated directly but can be used as reference types.
  • Java supports multiple inheritance through interfaces, allowing a class to implement multiple interfaces simultaneously.
  • Java provides different types of interfaces, including Marker Interface, Functional Interface, Normal Interface, and Nested Interface, each serving different purposes.
  • An interface can extend another interface using extends, allowing hierarchical interface structures for better code organization.
  • Interfaces promote loose coupling, reusability, and testability but also introduce code duplication, maintenance challenges, and minor performance overhead due to dynamic method resolution.

Continue learning Java from basics to advanced topics in our complete Java Tutorial.
Practice what you’ve learned with topic-wise questions on our Java MCQs.

👉 For weekly java practice, exam, and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels

advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations