Home  >  Article  >  Java  >  What is the difference between abstract class and interface in Java?

What is the difference between abstract class and interface in Java?

王林
王林forward
2023-04-22 20:16:061729browse

Difference

1. Abstract classes and subclasses have an is relationship, and interfaces have a like relationship. Abstract classes have a high degree of coupling and have better reusability and maintenance. Sexually, the interface is the opposite.

2. Abstraction can only be inherited alone, and multiple interfaces and subclasses can be implemented.

Abstracts can have non-constant member variables, interfaces can only have constant member variables, interfaces have default methods after 1.8, and abstractions have member methods.

Example

Examples of doors and alarms: doors have two actions: open() and close(). At this time, we can define them through abstract classes and interfaces. Define this abstract concept:

abstract class Door {
 
    public abstract void open();
 
    public abstract void close();
 
}

The above is the detailed content of What is the difference between abstract class and interface in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete