Java abstract interface
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Java interfaces are a fundamental part of the Java programming language and provide a contract for classes to implement. An abstract interface in Java refers to an interface that defines a set of method signatures without implementations, encouraging a form of abstraction and polymorphism. In this article, we will explore Java interfaces, including their purpose, syntax, and usage with examples. We will also discuss how they relate to abstract classes and when to use each approach.
Understanding Java Interfaces
What is an Interface?
In Java, an interface is a reference type, similar to a class, that can contain constants, method signatures, default methods, static methods, and nested types. However, it cannot contain instance fields or methods with bodies (with the exception of default and static methods in Java 8 and later). Interfaces help in defining a contract that other classes can implement.
Declaring an Interface
An interface is declared using the interface keyword. Here's a simple example:
Classes that implement this interface must provide concrete implementations of the start and stop methods.
Implementing an Interface
To implement an interface, a class uses the implements keyword and provides definitions for all of the interface's methods:
Abstract Interface vs. Abstract Class
An abstract class can have defined and undefined methods (abstract methods), while an interface mainly consists of undefined methods until Java 8, where default and static methods were introduced.
Abstract Class Example
Differences Between Abstract Classes and Interfaces
| Feature | Abstract Class | Interface |
| Declaration | abstract keyword | interface keyword |
| Method Implementations | Can have method definitions | Cannot have method implementations (except default and static methods) |
| Access Modifiers | Can have public, protected, and private methods and variables | All methods are implicitly public |
| Multiple Inheritance | Cannot be inherited by multiple classes | Can be implemented by multiple classes |
| Constructor Usage | Can have constructors | Cannot have constructors |
Advanced Interface Features
Default Methods
Introduced in Java 8, default methods allow interfaces to have method bodies. This feature provides additional flexibility, enabling backward compatibility and evolving interfaces without breaking existing implementations.
Static Methods
Static methods in interfaces, also introduced in Java 8, are useful for defining utility methods related to interfaces.
Functional Interfaces and Lambda Expressions
Java 8 also introduced the concept of functional interfaces, which are interfaces with a single abstract method. These are crucial for working with lambda expressions and the Stream API.
Use Cases and Considerations
When to Use Interfaces
- Multiple Inheritance: When you need multiple inheritance capabilities.
- Polymorphism: Enabling polymorphic behavior across various classes.
- API Contracts: Defining a strict contract for classes to adhere to.
When to Use Abstract Classes
- Shared Code: When you have common methods or fields that subclasses should inherit.
- Non-Abstract Methods: When some methods can have partial implementations.
Conclusion
Interface abstraction in Java promotes design flexibility, clean separation of concerns, and robust architectural patterns. The addition of default and static methods provides an improved mechanism for interface evolution. Whether to use interfaces, abstract classes, or a combination of both depends on the specific requirements and architectural goals of your software project.
Java developers should familiarize themselves with both constructs to make informed design decisions that leverage the strengths and capabilities of the Java language.
Related reading

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.