Home> Java> javaTutorial> body text

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

王林
Release: 2024-05-01 12:15:02
Original
662 people have browsed it

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(); }
Copy after login

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(); }
Copy after login

Practical case

Interface

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

public interface Shape { double getArea(); double getPerimeter(); }
Copy after login

Use this interface to create Rectangle and Circle classes:

public class Rectangle implements Shape { // ... } public class Circle implements Shape { // ... }
Copy after login

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(); }
Copy after login

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 { // ... }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!