Home  >  Article  >  Java  >  What are the characteristics of abstract classes and interfaces?

What are the characteristics of abstract classes and interfaces?

王林
王林Original
2020-07-13 14:04:294161browse

Characteristics of abstract classes: 1. There can be constructors in abstract classes; 2. There can be private methods and constructors in abstract classes. Characteristics of interfaces: There are only method declarations in the interface, but no method bodies; 2. There are only constants in the interface; 3. The interface can be inherited multiple times.

What are the characteristics of abstract classes and interfaces?

Characteristics of abstract classes:

(Recommended learning: java entry program)

1 , methods can be constructed in abstract classes.

2. Ordinary properties, methods, static properties and methods can exist in abstract classes.

3. Abstract methods have no method body.

4. Abstract classes can have private methods (non-abstract methods) and constructors.

5. If there is an abstract method in a class, then the current class must be an abstract class; there may not be an abstract method in an abstract class.

6. The abstract method in an abstract class needs to be implemented by a subclass. If the subclass does not implement it, the subclass also needs to be defined as abstract.

7. Abstract classes can only be inherited individually. Abstract classes can inherit interfaces or concrete classes (only some methods can be implemented when inheriting interfaces).

Features of the interface:

(Video tutorial recommendation: java video tutorial)

1. There are only method declarations in the interface, and there are no methods. body. (Java8 interfaces can have instance methods)

2. There are only constants in the interface, because the defined variables will be added by default public static final (must be initialized and cannot be changed) during compilation.

3. Methods in interfaces are always modified by public (only).

4. There is no constructor method in the interface, and the objects of the interface cannot be instantiated.

5. Interfaces can be inherited multiple times.

6. The methods defined in the interface need to be implemented by an implementation class. If the implementation class cannot implement all the methods in the interface, the implementation class is defined as an abstract class.

7. Interfaces can inherit interfaces, but not classes (abstract classes and ordinary classes).

The above is the detailed content of What are the characteristics of abstract classes and interfaces?. 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