Java Interface Practice Questions with Solutions

An Interface in Java is a blueprint of a class that defines a set of methods a class must implement. Interfaces are one of the most important features of Object-Oriented Programming (OOP) because they provide 100% abstraction (before Java 8) and help achieve multiple inheritance in Java.

Unlike abstract classes, an interface contains method declarations without implementation (except default and static methods introduced in Java 8).

In simple words:

An interface defines “what a class should do,” while the implementing class defines “how it should do it.”


Why Do We Use Interfaces?

Interfaces are used to:

  • Achieve abstraction
  • Support multiple inheritance
  • Reduce code dependency
  • Improve code reusability
  • Build loosely coupled applications
  • Create flexible software architecture

Interfaces are heavily used in:

  • Spring Boot
  • Hibernate
  • Android Development
  • Java Enterprise Applications
  • Banking Systems
  • Payment Gateways
  • Collection Framework
  • JDBC

What is an Interface?

An interface is declared using the interface keyword.

Example:

interface Animal {

    void sound();

}

A class implements an interface using the implements keyword.

Example:

class Dog implements Animal {

    public void sound() {

        System.out.println("Dog Barks");

    }

}

Rules of Interfaces

  • Declared using the interface keyword.
  • Cannot create objects directly.
  • Methods are public and abstract by default.
  • Variables are public static final by default.
  • A class uses the implements keyword.
  • Java supports implementing multiple interfaces.

Interface vs Abstract Class

InterfaceAbstract Class
Uses interface keywordUses abstract class
Supports multiple inheritanceSingle inheritance only
Variables are constantsCan contain instance variables
Methods are abstract by defaultCan contain abstract and concrete methods
Uses implementsUses extends

Real-World Examples of Interfaces

Interfaces are commonly used in:

  • Payment Systems
  • Online Banking
  • Authentication Systems
  • Notification Services
  • Vehicle Controls
  • Database Connectivity
  • Java Collections Framework
  • Android Applications

Before Learning This Chapter

You should already understand:

  • Classes
  • Objects
  • Constructors
  • Inheritance
  • Polymorphism
  • Abstraction

In this chapter, you’ll solve practical Java Interface programs that are frequently asked in coding interviews, university exams, and Java developer assessments. Java Interface practice questions with solutions help to understand the cocepts.


1. Java Program to Create a Simple Interface

Problem Statement

Write a Java program to create a simple interface and implement it in a class.

Java Solution

interface Animal {

    void sound();

}

class Dog implements Animal {

    @Override
    public void sound() {

        System.out.println("Dog Barks");

    }

}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.sound();

    }

}

Sample Output

Dog Barks

Explanation

The Animal interface defines the sound() method.

The Dog class implements the interface and provides the implementation of the method.

Since interfaces cannot be instantiated directly, we create an object of the implementing class.

Concepts Covered

  • Interface
  • implements Keyword
  • Method Implementation
  • Object-Oriented Programming

2. Java Program to Demonstrate Multiple Interface Implementation

Problem Statement

Write a Java program where a class implements multiple interfaces.

Java Solution

interface Printer {

    void print();

}

interface Scanner {

    void scan();

}

class AllInOneMachine implements Printer, Scanner {

    @Override
    public void print() {

        System.out.println("Printing Document");

    }

    @Override
    public void scan() {

        System.out.println("Scanning Document");

    }

}

public class Main {

    public static void main(String[] args) {

        AllInOneMachine machine = new AllInOneMachine();

        machine.print();

        machine.scan();

    }

}

Sample Output

Printing Document
Scanning Document

Explanation

Java does not support multiple inheritance using classes.

However, a class can implement multiple interfaces.

This is one of the biggest advantages of interfaces.

Concepts Covered

  • Multiple Interfaces
  • implements Keyword
  • Multiple Inheritance
  • Interface Implementation

3. Java Program to Demonstrate Interface Variables

Problem Statement

Write a Java program to demonstrate interface variables.

Java Solution

interface College {

    String NAME = "VSIT Computer Education";

}

class Student implements College {

    void display() {

        System.out.println("College : " + NAME);

    }

}

public class Main {

    public static void main(String[] args) {

        Student student = new Student();

        student.display();

    }

}

Sample Output

College : VSIT Computer Education

Explanation

All variables declared inside an interface are automatically:

  • public
  • static
  • final

Therefore, they behave like constants and can be accessed directly by implementing classes.

Concepts Covered

  • Interface Variables
  • Constants
  • Static Variables
  • Final Variables

4. Java Program to Demonstrate Interface with Default Methods

Problem Statement

Write a Java program to demonstrate the use of default methods in an interface.

Java Solution

interface Animal {

    default void sleep() {

        System.out.println("Animal is Sleeping");

    }

    void sound();

}

class Dog implements Animal {

    @Override
    public void sound() {

        System.out.println("Dog Barks");

    }

}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.sound();

        dog.sleep();

    }

}

Sample Output

Dog Barks
Animal is Sleeping

Explanation

From Java 8, interfaces can contain default methods.

A default method:

  • Has a method body.
  • Is inherited automatically by implementing classes.
  • Can be overridden if required.

This feature allows developers to add new functionality to interfaces without breaking existing implementations.

Concepts Covered

  • Interface
  • Default Methods
  • Java 8 Features
  • Method Inheritance

5. Java Program to Demonstrate Interface with Static Methods

Problem Statement

Write a Java program to demonstrate static methods inside an interface.

Java Solution

interface Calculator {

    static void message() {

        System.out.println("Welcome to Java Interface");

    }

}

public class Main {

    public static void main(String[] args) {

        Calculator.message();

    }

}

Sample Output

Welcome to Java Interface

Explanation

From Java 8, interfaces can contain static methods.

These methods:

  • Belong to the interface itself.
  • Cannot be overridden.
  • Must be called using the interface name.

Example:

Calculator.message();

Concepts Covered

  • Interface
  • Static Methods
  • Java 8 Features
  • Interface Utilities

6. Java Program to Demonstrate Functional Interface

Problem Statement

Write a Java program to demonstrate a Functional Interface.

Java Solution

@FunctionalInterface
interface Greeting {

    void sayHello();

}

class Welcome implements Greeting {

    @Override
    public void sayHello() {

        System.out.println("Welcome to Java Programming");

    }

}

public class Main {

    public static void main(String[] args) {

        Welcome welcome = new Welcome();

        welcome.sayHello();

    }

}

Sample Output

Welcome to Java Programming

Explanation

A Functional Interface contains exactly one abstract method.

It is mainly used with:

  • Lambda Expressions
  • Method References
  • Stream API

The @FunctionalInterface annotation tells the compiler that the interface must contain only one abstract method.

Concepts Covered

  • Functional Interface
  • @FunctionalInterface
  • Java 8 Features
  • Lambda Foundation

7. Java Program to Create a Vehicle Interface

Problem Statement

Write a Java program to create a Vehicle interface and implement it using a Car class.

Java Solution

interface Vehicle {

    void start();

}

class Car implements Vehicle {

    @Override
    public void start() {

        System.out.println("Car Starts Successfully");

    }

}

public class Main {

    public static void main(String[] args) {

        Car car = new Car();

        car.start();

    }

}

Sample Output

Car Starts Successfully

Explanation

The Vehicle interface defines a contract.

The Car class implements the interface and provides the actual implementation of the start() method.

Concepts Covered

  • Interface
  • implements Keyword
  • Vehicle Example
  • Method Implementation

8. Java Program to Create a Payment Interface

Problem Statement

Write a Java program to create a payment system using interfaces.

Java Solution

interface Payment {

    void pay();

}

class UPI implements Payment {

    @Override
    public void pay() {

        System.out.println("Payment Successful using UPI");

    }

}

public class Main {

    public static void main(String[] args) {

        UPI payment = new UPI();

        payment.pay();

    }

}

Sample Output

Payment Successful using UPI

Explanation

The interface provides a common payment contract.

Different payment methods such as:

  • UPI
  • Credit Card
  • Debit Card
  • Net Banking

can implement the same interface while providing their own payment logic.

Concepts Covered

  • Interface
  • Payment System
  • Real-World Example
  • Method Implementation

9. Java Program to Demonstrate Marker Interface

Problem Statement

Write a Java program to demonstrate a Marker Interface.

Java Solution

interface Student {

}

class Rahul implements Student {

    void display() {

        System.out.println("Student Record Available");

    }

}

public class Main {

    public static void main(String[] args) {

        Rahul student = new Rahul();

        student.display();

    }

}

Sample Output

Student Record Available

Explanation

A Marker Interface is an interface without any methods or variables.

It is used to provide special information to the compiler or JVM.

Examples in Java include:

  • Serializable
  • Cloneable
  • Remote

Concepts Covered

  • Marker Interface
  • Empty Interface
  • Java Built-in Marker Interfaces

10. Java Program to Implement Multiple Interfaces in a Banking System

Problem Statement

Write a Java program where a class implements multiple interfaces in a banking application.

Java Solution

interface Deposit {

    void deposit();

}

interface Withdraw {

    void withdraw();

}

class BankAccount implements Deposit, Withdraw {

    @Override
    public void deposit() {

        System.out.println("Money Deposited");

    }

    @Override
    public void withdraw() {

        System.out.println("Money Withdrawn");

    }

}

public class Main {

    public static void main(String[] args) {

        BankAccount account = new BankAccount();

        account.deposit();

        account.withdraw();

    }

}

Sample Output

Money Deposited
Money Withdrawn

Explanation

The BankAccount class implements two interfaces:

  • Deposit
  • Withdraw

This demonstrates multiple inheritance using interfaces, which is fully supported in Java.

Concepts Covered

  • Multiple Interfaces
  • Banking Example
  • Interface Implementation
  • Multiple Inheritance

11. Java Program to Create an Employee Interface

Problem Statement

Write a Java program to create an Employee interface and implement it using a Developer class.

Java Solution

interface Employee {

    void work();

}

class Developer implements Employee {

    @Override
    public void work() {

        System.out.println("Developer is Writing Java Code");

    }

}

public class Main {

    public static void main(String[] args) {

        Developer developer = new Developer();

        developer.work();

    }

}

Sample Output

Developer is Writing Java Code

Explanation

The Employee interface defines the work() method.

The Developer class implements the interface and provides the required implementation.

This approach allows different employee roles to define their own working behavior while following the same contract.

Concepts Covered

  • Interface
  • Employee Example
  • Method Implementation
  • Object-Oriented Programming

12. Java Program to Create a Notification Interface

Problem Statement

Write a Java program to create a notification system using interfaces.

Java Solution

interface Notification {

    void send();

}

class EmailNotification implements Notification {

    @Override
    public void send() {

        System.out.println("Email Notification Sent");

    }

}

public class Main {

    public static void main(String[] args) {

        EmailNotification email = new EmailNotification();

        email.send();

    }

}

Sample Output

Email Notification Sent

Explanation

The interface defines a common notification contract.

Different notification types such as:

  • Email
  • SMS
  • Push Notification
  • WhatsApp

can implement the same interface with different behaviors.

Concepts Covered

  • Interface
  • Notification System
  • Method Implementation
  • Real-World Example

13. Java Program to Demonstrate Default Method Overriding

Problem Statement

Write a Java program to override a default method provided by an interface.

Java Solution

interface Animal {

    default void sound() {

        System.out.println("Animal Makes Sound");

    }

}

class Dog implements Animal {

    @Override
    public void sound() {

        System.out.println("Dog Barks");

    }

}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.sound();

    }

}

Sample Output

Dog Barks

Explanation

Default methods can be overridden by implementing classes.

This allows developers to customize default behavior whenever needed.

Concepts Covered

  • Default Methods
  • Method Overriding
  • Java 8 Features
  • Interface Customization

14. Java Program to Demonstrate a Smart Device Interface

Problem Statement

Write a Java program to demonstrate interfaces using a smart device example.

Java Solution

interface SmartDevice {

    void powerOn();

}

class SmartTV implements SmartDevice {

    @Override
    public void powerOn() {

        System.out.println("Smart TV Powered On");

    }

}

public class Main {

    public static void main(String[] args) {

        SmartTV television = new SmartTV();

        television.powerOn();

    }

}

Sample Output

Smart TV Powered On

Explanation

The interface defines a common operation (powerOn()).

Different smart devices such as:

  • Smart TV
  • Smart Fan
  • Smart AC
  • Smart Speaker

can provide their own implementation.

This design makes applications flexible and easy to expand.

Concepts Covered

  • Interface
  • Smart Device Example
  • Method Implementation
  • Real-World Java Program

15. Java Program to Demonstrate Interface Polymorphism

Problem Statement

Write a Java program to demonstrate Interface Polymorphism.

Java Solution

interface Animal {

    void sound();

}

class Dog implements Animal {

    @Override
    public void sound() {

        System.out.println("Dog Barks");

    }

}

public class Main {

    public static void main(String[] args) {

        Animal animal = new Dog();

        animal.sound();

    }

}

Sample Output

Dog Barks

Explanation

Here, the reference variable belongs to the interface (Animal), while the object belongs to the implementing class (Dog).

This demonstrates Interface Polymorphism, where Java executes the appropriate implementation during runtime.

Interface polymorphism is widely used in enterprise Java applications, Spring Framework, and Android development.

Concepts Covered

  • Interface Polymorphism
  • Runtime Polymorphism
  • Interface Reference
  • Dynamic Method Dispatch

Chapter Summary

In this chapter, you learned about Java Interfaces, one of the most important concepts in Object-Oriented Programming (OOP). An interface defines a contract that implementing classes must follow, making Java applications more flexible, modular, and maintainable.

Unlike abstract classes, interfaces are primarily used to achieve complete abstraction and multiple inheritance. They allow unrelated classes to share a common behavior without sharing implementation.

Throughout this chapter, you practiced:

  • Creating Interfaces
  • Implementing Interfaces
  • Multiple Interface Implementation
  • Interface Variables
  • Default Methods
  • Static Methods
  • Functional Interfaces
  • Marker Interfaces
  • Interface Polymorphism
  • Real-world Examples:
    • Vehicle System
    • Banking System
    • Employee Management
    • Payment Gateway
    • Notification System
    • Smart Devices

Interfaces are widely used in modern Java frameworks such as Spring Boot, Hibernate, Android, and Java Enterprise Applications.


Key Takeaways

  • An interface is declared using the interface keyword.
  • A class implements an interface using the implements keyword.
  • Interfaces provide abstraction.
  • Java supports multiple inheritance through interfaces.
  • Interface methods are public and abstract by default.
  • Interface variables are public static final.
  • Java 8 introduced:
    • Default Methods
    • Static Methods
  • Functional Interfaces contain only one abstract method.
  • Marker Interfaces contain no methods.
  • Interface Polymorphism improves flexibility and scalability.

Frequently Asked Questions (FAQs)

1. What is an interface in Java?

An interface is a blueprint that defines methods a class must implement. It specifies what should be done, while the implementing class defines how it should be done.


2. Why are interfaces used in Java?

Interfaces are used to:

  • Achieve abstraction
  • Support multiple inheritance
  • Improve code reusability
  • Reduce coupling
  • Build scalable applications

3. How do you implement an interface?

A class implements an interface using the implements keyword.

Example:

interface Animal {

    void sound();

}

class Dog implements Animal {

    @Override
    public void sound() {

        System.out.println("Dog Barks");

    }

}

4. Can an interface contain method implementations?

Yes.

Since Java 8, interfaces can contain:

  • Default Methods
  • Static Methods

Since Java 9, interfaces can also contain private methods.


5. What is a Functional Interface?

A Functional Interface contains exactly one abstract method.

It is mainly used with:

  • Lambda Expressions
  • Method References
  • Stream API

Example:

@FunctionalInterface
interface Greeting {

    void hello();

}

6. What is a Marker Interface?

A Marker Interface is an interface without any methods or variables.

Examples include:

  • Serializable
  • Cloneable
  • Remote

These interfaces provide metadata to the JVM or compiler.


7. What is Interface Polymorphism?

Interface Polymorphism allows an interface reference to refer to an implementing class object.

Example:

Animal animal = new Dog();

animal.sound();

The correct method is selected during runtime.


8. Where are interfaces used in real-world applications?

Interfaces are commonly used in:

  • Spring Boot
  • Hibernate
  • Android Apps
  • Payment Gateways
  • Banking Systems
  • Notification Services
  • Java Collections Framework
  • Enterprise Applications
  • Microservices
  • JDBC Drivers

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top