Java Interface
发布时间:2026-09-19 | 浏览:1
Interview Questions
Java Collection
An interface in Java is a blueprint that defines a set of methods a class must implement without providing full implementation details. It helps achieve abstraction by focusing on what a class should do rather than how it does it. Interfaces also support multiple inheritance in Java.
A class must implement all abstract methods of an interface.
All variables in an interface are public, static, and final by default.
Interfaces can have default, static, and private methods (Java 8+ and 9+).
Explanation: TestInterface declares the constant a and the abstract method display(). TestClass implements the interface and provides the implementation of display(). In main(), a TestClass object is created and its display() method is called. The value of a is accessed through the object.
Notes: The interface variable a is automatically public static final, and display() is automatically public abstract .
Relationship Between Class and Interface
A class can extend another class and similarly, an interface can extend another interface. However, only a class can implement an interface and the reverse (an interface implementing a class) is not allowed.
An interface can extend multiple interfaces.
An interface cannot implement a class.
A class cannot extend an interface.
Use case of Class
Use a class when you need to represent a real-world entity with attributes (fields) and behaviors (methods).
Use a class when you need to create objects that hold state and perform actions
Classes are used for defining templates for objects with specific functionality and properties.
Use case of Interface
Use an interface when you need to define a contract for behavior that multiple classes can implement.
Interface is ideal for achieving abstraction and multiple inheritance.
Implementing an Interface
A class uses the implements keyword to implement an interface.
Consider vehicles such as bicycles and bikes. Both can change gears, increase speed, and apply brakes, but each class can provide its own implementation.
Explanation: Vehicle defines three common operations. Both Bicycle and Bike implement these methods. The two classes maintain their own speed and gear values. Although both classes follow the same Vehicle contract, each class can provide its own implementation.
Multiple Inheritance Using Interface
Java does not support multiple inheritance with classes to avoid ambiguity, but it supports multiple inheritance using interfaces.
A class can implement more than one interface.
Multiple interfaces are separated using commas.
The implements keyword is used only once.
The implementing class must provide implementations for all inherited abstract methods.
A class can extend one class and implement multiple interfaces at the same time.
Explanation: Add defines the add() contract and Sub defines the sub() contract. Calculator implements both interfaces using implements Add, Sub. Therefore, it must provide implementations for both methods. This demonstrates how a class can implement multiple interfaces.
New Features Added in Interfaces in JDK 8
There are certain features added to Interfaces in JDK 8 update mentioned below:
1. Default Methods
Interfaces can define methods with default implementations.
Useful for adding new methods to interfaces without breaking existing implementations.
Explanation: display() already has an implementation in the interface. Therefore, TestClass does not need to override it and can directly use the inherited default method.
2. Static Methods
Interfaces can now include static methods.
These methods are called directly using the interface name and are not inherited by implementing classes.
Another feature that was added in JDK 8 is that we can now define static methods in interfaces that can be called independently without an object. These methods are not inherited.
Explanation: The static method belongs to the interface itself, so it is called using the interface name. It is not inherited by implementing classes.
3. Functional Interface
Functional interfaces can be used with lambda expressions or method references.
The @FunctionalInterface annotation can be used to indicate that an interface is a functional interface, although it’s optional.
Explanation: Calculator is a functional interface because it contains one abstract method, compute(). The @FunctionalInterface annotation tells the compiler that the interface is intended to have exactly one abstract method.
New Features Added in Interfaces in JDK 9
From Java 9 onwards, interfaces can contain the following also:
1. Private Methods
Interface can now include private methods.
Private methods are defined within the interface but it cannot be accessed by the implementing classes.
Private methods cannot be overridden by implementing classes as they are not inherited.
Explanation: startEngine() is private, so it cannot be called directly by Car. The default method drive() calls it internally. This allows interfaces to reuse common logic without exposing that helper method to implementing classes.
Extending Interfaces
One interface can inherit another by the use of keyword extends. When a class implements an interface that inherits another interface, it must provide an implementation for all methods required by the interface inheritance chain.
Explanation: B extends A, so B inherits the contracts of A and adds method3(). Since GFG implements B, it must provide implementations for method1(), method2(), and method3().
Related Article
Class vs Interfaces
Introduction 4 min read
Variables 2 min read
Data Types 7 min read
Operators 7 min read
Decision Making 5 min read
Methods 7 min read
Arrays 7 min read
Strings 5 min read
Introduction 6 min read
Constructors 6 min read
Encapsulation 3 min read
Inheritance 8 min read
Polymorphism 5 min read
Abstraction 5 min read
Interfaces 8 min read
Collection class 9 min read
Collection Interface 5 min read
List Interface 8 min read
Set Interface 5 min read
Queue Interface 5 min read
Map Interface in Java 6 min read
Iterator 4 min read
Exception handling 6 min read
Java Multithreading Tutorial 3 min read
Synchronization in Java 8 min read
File handling 6 min read
Method References 3 min read
Java 8 Stream Tutorial 8 min read
Memory Management 4 min read
Garbage Collection in Java 6 min read
Networking 6 min read
JDBC 5 min read
Interview Questions 15+ min read
Programs 6 min read
Practice Problems 5 min read
Quiz 1 min read
Project Ideas 15+ min read
Java Backend Course 2 min read
Placement 360 Course 2 min read