Home >Java >javaTutorial >The difference between interface and abstract class

The difference between interface and abstract class

angryTom
angryTomOriginal
2020-02-17 14:25:545910browse

The difference between interface and abstract class

The difference between interface and abstract class

There is a big difference between interface and abstract class, and the choice of them even reflects Whether the understanding of the nature of the problem domain and the design intention is correct and reasonable.

Abstract class

Interface

Instantiation

Cannot

Cannot

Class

An inheritance relationship, a class can only use the inheritance relationship once. Multiple inheritance can be achieved by inheriting multiple interfaces

A class can implement multiple interfaces

Data members

Can have its own

Static cannot be modified, that is, it must be static final, generally not defined here

Method

Can be private, non-abstract method, must be implemented

cannot be private, default Is public, abstract type

Variable

can be private, the default is friendly type, its value can be in It can be redefined in a subclass or reassigned.

cannot be private. The default is public static final type, and its initial value must be given. It cannot be redefined or changed in the implementation class. its value.

Design concept

represents the "is-a" relationship

represents the "like-a" relationship

To achieve

requires inheritance, use extends

To use implementations

1. Similarities

A. Both are abstract classes and cannot be instantiated.

B. Both interface implementation classes and subclasses of abstract class must implement the declared abstract methods.

Recommended learning: java tutorial

2. Differences

A. The interface needs to be implemented, and implements must be used. Abstract class needs to be inherited and uses extends.

B. A class can implement multiple interfaces, but a class can only inherit one abstract class.

C. Interface emphasizes the implementation of specific functions, while abstract class emphasizes ownership relationships.

D. Although both the interface implementation class and the subclasses of abstract class must implement the corresponding abstract methods, the implementation forms are different. Every method in the interface is an abstract method, which is only declared (declaration, without method body), and the implementation class must implement it. Subclasses of abstract class can be implemented selectively.

3. Application occasions of interface

A. Classes need specific interfaces for coordination, regardless of how they are implemented.

B. It exists as an identifier that can realize a specific function, or it can be a pure identifier without any interface methods.

C. A group of classes needs to be treated as a single class, and the caller only contacts this group of classes through the interface.

D. Specific multiple functions need to be implemented, and these functions may have no connection at all.

4. Application occasions of abstract class

In a word, when both a unified interface and instance variables or default methods are needed, then Can use it. The most common ones are:

A. Define a set of interfaces, but do not want to force each implementation class to implement all interfaces. You can use abstract class to define a set of method bodies, or even empty method bodies, and then let subclasses choose the methods they are interested in to cover.

B. In some cases, pure interfaces alone cannot satisfy the coordination between classes, and variables representing states in the class are also required to distinguish different relationships. The intermediary role of abstract can satisfy this very well.

C. Standardizes a set of mutually coordinated methods, some of which are common, independent of state, and can be shared without the need for subclasses to implement them separately; while other methods require each subclass to implement it according to its own A specific state to achieve a specific function.

For more programming tutorials, please continue to pay attention to the PHP Chinese website.         

The above is the detailed content of The difference between interface and abstract class. 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