Home >Java >javaTutorial >When to use interfaces and when to use abstract classes in Java

When to use interfaces and when to use abstract classes in Java

王林
王林Original
2024-05-01 12:15:02758browse

When to use the interface: Define shared functions, implemented by different types of objects. Define callback interfaces (such as event listeners). Implement multiple inheritance. When to use abstract classes: Define common functions implemented through inheritance. Implement single inheritance and polymorphism. Define a protected method or field.

Java 中何时使用接口何时使用抽象类

When to use interfaces and abstract classes in Java

In Java, interfaces and abstract classes are both used to define objects Abstract concept of behavior. Despite their similarities, there are clear differences in their purpose of use.

Interface

An interface is a contract that contains method signatures (that is, method declarations without implementation). It defines the set of public methods that a class must implement.

When to use interfaces?

  • Define common functionality that can be shared by objects of different types.
  • When you need to define a callback interface, such as an event listener.
  • When multiple inheritance needs to be implemented (not supported in Java).
public interface Animal {
    void eat();
    void sleep();
}

Abstract class

An abstract class is a partially abstract class that contains a combination of method implementations and abstract methods. Abstract classes cannot be instantiated, but can be inherited by subclasses.

When to use abstract classes?

  • Define common functions that can only be achieved through inheritance.
  • When you need to implement single inheritance and polymorphism.
  • When you need to define protected methods or fields.
public abstract class Mammal {
    public void giveBirth() { ... }
    public abstract void makeSound();
}

Practical case

Interface

Create a Shape interface defined for calculating area and perimeter Public methods:

public interface Shape {
    double getArea();
    double getPerimeter();
}

Use this interface to create Rectangle and Circle classes:

public class Rectangle implements Shape {
    // ...
}

public class Circle implements Shape {
    // ...
}

Abstract class

Create a Shape abstract class defined for Protected methods for calculating area and perimeter:

public abstract class Shape {
    protected double area;
    protected double perimeter;

    public abstract double getArea();
    public abstract double getPerimeter();
}

Use this abstract class to create Rectangle and Circle classes, overriding the getArea() and getPerimeter() methods:

public class Rectangle extends Shape {
    // ...
}

public class Circle extends Shape {
    // ...
}

The above is the detailed content of When to use interfaces and when to use abstract classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn