Home > Java > javaTutorial > body text

The definition and difference between abstract classes and interfaces

零下一度
Release: 2017-07-18 17:52:49
Original
1282 people have browsed it

1. What is an abstract class?

If multiple classes have behaviors of the same nature, such as people walking and animals walking, walking is a behavior of the same nature in two different classes of humans and animals. In order to facilitate management, These behaviors can be abstracted and concentrated into one class, thus producing a class that combines the common behaviors of multiple classes. This class is abstract class.

2. What is an interface?

The two classes not only have behaviors of the same nature, but also behaviors of different natures. For example, humans sleep, birds sleep, and birds fly. Sleeping is common to humans and birds Behavior can be defined in an abstract class, but flying is not a common behavior of humans and birds and cannot be defined in an abstract class. This non-shared behavior can be defined in a special Among classes, this special class is the interface.

3. Difference

Taking n classes as the research object, the common behaviors of n classes are defined in abstract classes, and some classes share The behavior where the number of shared classes does not reach n is defined in the interface.

Abstract classes reflect the overall commonality, while interfaces reflect local commonality.

The choice of abstract class and interface depends on the scope of concern. If the scope of concern is small and the objects abstracted from this small scope all behave the same, you can use abstract classes to manage common behaviors. If the scope of concern is relatively large, and within this large scope, certain behaviors are shared by only some objects, then These behaviors can only be defined in the interface.

The above is the detailed content of The definition and difference between abstract classes and interfaces. 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
Popular Tutorials
More>
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!