Java
Abstract Interface
Programming
Software Development
Object-Oriented Programming

Java abstract interface

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

java
1public interface Vehicle {
2    // Method signatures
3    void start();
4    void stop();
5}

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:

java
1public class Car implements Vehicle {
2
3    @Override
4    public void start() {
5        System.out.println("Car starting");
6    }
7
8    @Override
9    public void stop() {
10        System.out.println("Car stopping");
11    }
12}

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

java
1public abstract class AbstractVehicle {
2    abstract void start();
3    abstract void stop();
4
5    void honk() {
6        System.out.println("Honking!");
7    }
8}

Differences Between Abstract Classes and Interfaces

FeatureAbstract ClassInterface
Declarationabstract keywordinterface keyword
Method ImplementationsCan have method definitionsCannot have method implementations (except default and static methods)
Access ModifiersCan have public, protected, and private methods and variablesAll methods are implicitly public
Multiple InheritanceCannot be inherited by multiple classesCan be implemented by multiple classes
Constructor UsageCan have constructorsCannot 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.

java
1public interface Vehicle {
2    void start();
3    void stop();
4    
5    default void honk() {
6        System.out.println("Honking default!");
7    }
8}

Static Methods

Static methods in interfaces, also introduced in Java 8, are useful for defining utility methods related to interfaces.

java
1public interface VehicleUtils {
2    static void checkEngine() {
3        System.out.println("Checking engine from static method.");
4    }
5}

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.

java
1@FunctionalInterface
2public interface Validator {
3    boolean validate(String data);
4}
5
6// Using a lambda expression
7Validator emailValidator = email -> email.contains("@");

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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.