Java Interfaces vs. Abstract Classes: Differences and Implementation, A Must-Know for Beginners

In Java, Interfaces and Abstract Classes are important concepts in object-oriented programming, and they are also a confusing point for beginners. Both are used to achieve code reuse and polymorphism, but their design purposes and usage scenarios are截然不同. This article will help you quickly distinguish between them and master their core usage with simple examples and clear logic.

1. Interface: “Contract” of Behavior

What is an Interface?
An interface is a special reference type that defines a set of abstract methods (only before Java 8) and constant specifications, without containing specific method implementations. The core role of an interface is to specify the behavior of classes, enabling different classes to reuse the same set of behavior logic by implementing the interface and achieve “polymorphism”.

Characteristics of Interfaces
1. Declaration Method: Declared using the interface keyword (e.g., defining a “flyable” interface):

   // Define a "flyable" interface
   interface Flyable {
       void fly(); // Abstract method (default: public abstract)
       int MAX_HEIGHT = 1000; // Constant (default: public static final)
   }

(Before Java 8, interfaces could only have abstract methods and constants. After Java 8, interfaces allow default methods and static methods like default void land() { ... }, but beginners can focus on basic usage first.)

  1. Cannot be Instantiated: Interfaces themselves cannot be instantiated with new; they must be instantiated through implementing classes (using the implements keyword).

  2. Multiple Implementations: A class can implement multiple interfaces (Java supports multi-interface implementation), whereas a Java class can only inherit from one class. For example:

   class Bird implements Flyable, Singable {
       // Implement fly() from Flyable and sing() from Singable
   }

Interface Example
Suppose we define a Greeting interface, requiring all implementing classes to have a “greeting” behavior:

// Interface definition
interface Greeting {
    void greet(); // Abstract method, must be implemented
}

// Implementing class 1: Human greeting
class Person implements Greeting {
    @Override
    public void greet() {
        System.out.println("Hello, I am a human!");
    }
}

// Implementing class 2: Animal greeting (e.g., Cat)
class Cat implements Greeting {
    @Override
    public void greet() {
        System.out.println("Meow~ I am a cat!");
    }
}

// Test: Call different implementing classes through interface types
public class InterfaceTest {
    public static void main(String[] args) {
        Greeting person = new Person();
        Greeting cat = new Cat();
        person.greet(); // Output: Hello, I am a human!
        cat.greet();    // Output: Meow~ I am a cat!
    }
}

2. Abstract Class: Incomplete “Class Template”

What is an Abstract Class?
An abstract class is an “incomplete class” that can contain abstract methods (no implementation) and concrete methods (with implementation), as well as member variables. The core role of an abstract class is to define a class template, allowing subclasses to inherit and reuse its code while forcing subclasses to implement abstract methods.

Characteristics of Abstract Classes
1. Declaration Method: Declared using the abstract keyword (e.g., defining an “Animal” abstract class):

   // Define an "Animal" abstract class, abstracting common behaviors
   abstract class Animal {
       String name; // Member variable
       public Animal(String name) {
           this.name = name; // Constructor, called when initializing subclasses
       }
       public abstract void eat(); // Abstract method, must be implemented by subclasses
       public void sleep() { // Concrete method (with implementation)
           System.out.println(name + " is sleeping");
       }
   }
  1. Cannot be Instantiated: Abstract classes themselves cannot be instantiated with new; they must be instantiated through subclasses (using the extends keyword).

  2. Single Inheritance: A Java class can only inherit from one abstract class, whereas interfaces support multiple implementations.

Abstract Class Example
Inherit the Animal abstract class to implement specific animal behaviors:

// Inherit abstract class Animal
class Dog extends Animal {
    public Dog(String name) {
        super(name); // Call parent class constructor
    }
    @Override
    public void eat() {
        System.out.println(name + " eats bones"); // Implement abstract method
    }
}

// Test
public class AbstractClassTest {
    public static void main(String[] args) {
        Dog dog = new Dog("Puppy");
        dog.eat();   // Output: Puppy eats bones
        dog.sleep(); // Output: Puppy is sleeping
    }
}

3. Interface vs Abstract Class: Core Differences

Comparison Item Interface Abstract Class
Core Role Define “what can be done” behavior (e.g., “can fly”, “can greet”) Define “what is” class template (e.g., “Animal”, “Vehicle”)
Member Content Only abstract methods (before Java 8) and constants Can contain abstract methods, concrete methods, and member variables
Inheritance Classes use implements for multi-implementation (can implement multiple interfaces) Classes use extends for single inheritance (can only inherit one abstract class)
Constructor No constructor (cannot be instantiated) Has a constructor (used for subclass initialization)
Instantiation Must be instantiated through implementing classes Must be instantiated through subclasses
Keyword interface abstract class

4. How to Choose: Interface or Abstract Class?

  • Use Interface: When defining “behavior specifications” (e.g., Flyable interface, regardless of whether it is a bird or an airplane) or when multiple implementations are needed (a class requires multiple independent behaviors).
  • Use Abstract Class: When defining “class templates” (e.g., Animal abstract class, where subclasses share attributes and some methods like eat() but with different details) or when single inheritance and code reuse are needed (e.g., the parent class already has partial implementations, and the subclass only needs to extend).

5. Key Points for Beginners

  1. Neither Abstract Classes nor Interfaces Can Be Instantiated Directly: Only subclasses or implementing classes can create objects.
  2. Abstract Methods of Abstract Classes Must Be Implemented by Subclasses: Otherwise, the subclass must also be declared as abstract.
  3. Abstract Methods of Interfaces Are Default public abstract: When implemented, the method must use the public modifier (otherwise, the access permission is reduced, causing a compilation error).
  4. Abstract Classes Are Like “Family Templates” (e.g., the Animal family), while interfaces are like “skill certificates” (e.g., the Flyable certificate).

6. Summary

Interfaces and abstract classes are important tools in Java for achieving code reuse and polymorphism. Remember: Interfaces define “what can be done”, and abstract classes define “what is”. Beginners can start with simple scenarios: first distinguish between “inheritance relationship (is-a)” and “behavior relationship (can-do)”, then practice with example code, and you will quickly master the core differences between the two.

Xiaoye