Jenkov.com
Tutorials Books About
Java Language

1 Java Tutorial
2 What is Java?
3 Java Core Concepts
4 Java Syntax
5 Java Variables
6 Java Data Types
7 Java Arrays
8 Java Strings
9 Java Operations
10 Java if statements
11 Java Switch Statements
12 Java for Loops
13 Java while Loops
14 Java Classes
15 Java Fields
16 Java Methods
17 Java Constructors
18 Java Packages
19 Java Access Modifiers
20 Java Inheritance
21 Java Nested Classes
22 Java Abstract Classes
23 Java Interfaces
24 Java Interfaces vs. Abstract Classes
25 Java Enums
26 Java Annotations
27 Java Main Program
28 Java Lambda Expressions
29 Java Exercises




Java Interfaces vs. Abstract Classes


A question I get a lot is what the difference is between Java interfaces and abstract classes, and when to use each.

Java interfaces are used to decouple the interface of some component from the implementation. In other words, to make the classes using the interface independent of the classes implementing the interface. Thus, you can exchange the implementation of the interface, without having to change the class using the interface.

Abstract classes are typically used as base classes for extension by subclasses. Some programming languages use abstract classes to achieve polymorphism, and to separate interface from implementation, but in Java you use interfaces for that. Remember, a Java class can only have 1 superclass, but it can implement multiple interfaces. Thus, if a class already has a different superclass, it can implement an interface, but it cannot extend another abstract class. Therefore interfaces are a more flexible mechanism for exposing a common interface.

If you need to separate an interface from its implementation, use an interface. If you also need to provide a base class or default implementation of the interface, add an abstract class (or normal class) that implements the interface.

Here is an example showing a class referencing an interface, an abstract class implementing that interface, and a subclass extending the abstract class.

The blue class knows only the interface. The abstract class implements the interface, and the subclass inherits from the abstract class.
The blue class knows only the interface. The abstract class implements the interface, and the subclass inherits from the abstract class.

Below are the code examples from the text on Java Abstract Classes, but with an interface added which is implemented by the abstract base class. That way it resembles the diagram above.

First the interface:

public interface URLProcessor {

    public void process(URL url) throws IOException;
}

Second, the abstract base class:

public abstract class URLProcessorBase implements URLProcessor {

    public void process(URL url) throws IOException {
        URLConnection urlConnection = url.openConnection();
        InputStream input = urlConnection.getInputStream();

        try{
            processURLData(input);
        } finally {
            input.close();
        }
    }

    protected abstract void processURLData(InputStream input)
        throws IOException;

}

Third, the subclass of the abstract base class:

public class URLProcessorImpl extends URLProcessorBase {

    @Override
    protected void processURLData(InputStream input) throws IOException {
        int data = input.read();
        while(data != -1){
            System.out.println((char) data);
            data = input.read();
        }
    }
}

Fourth, how to use the interface URLProcessor as variable type, even though it is the subclass UrlProcessorImpl that is instantiated.

URLProcessor urlProcessor = new URLProcessorImpl();

urlProcessor.process(new URL("http://jenkov.com"));

Using both an interface and an abstract base class makes your code more flexible. It possible to implement simple URL processors simply by subclassing the abstract base class. If you need something more advanced, your URL processor can just implement the URLProcessor interface directly, and not inherit from URLProcessorBase.



Connect with me: Newsletter - Get all my free tips!