1. Abstract class
Abstract class describes the abstract world conceptually, abstract class depicts the characteristics of public behavior, and through the inheritance mechanism to its derived classes. Abstract classes Use the keyword abstract to modify the syntax as follows:
Abstract class class name {body;}
An abstract method is one in which some member methods in an abstract class do not have a specific implementation, only a method declaration. Abstract methods use the keyword abstract adornment, in the following format:
Public abstract return type method name ([parameter list]);
The specific rules for abstract classes and abstract methods are as follows:
1. When a class is decorated with the abstract keyword, the class is called an abstract class, and the method is called an abstract method when it is decorated with an abstract class.
2. Classes containing abstract methods must be declared as abstract classes, abstract classes must be inherited, and abstract methods must be overridden.
3. Abstract methods only need to be declared, not implemented.
4. There can be data members in an abstract class, there can be 0 or more abstract methods, or it can be a non-abstract method.
5. Abstract classes cannot be instantiated.
6.static, private, final methods cannot be abstract.
Classes of type 7.final cannot contain abstract methods.
8. Subclasses of an abstract class must implement all the abstract methods in the parent class, otherwise the class must be defined as an abstract class.
2. Interface
2.1 Definition of the interface
An interface is a special abstract class. The appearance of the interface makes up for the lack of single inheritance in Java, which can be used to accomplish some functions of multiple inheritance. In Java, the use of keyword interface to define interfaces, the definition of an excuse is similar to the definition of a class, is divided into the interface of the Declaration and interface body. But in the interface, all the methods are abstract, all variables are static constants, the syntax is as follows:
[Modifier] Interface Interface Name [extends parent interface list]{static constant; method declaration;}
Note:
1. The modifier for the interface is only public and the default modifier two.
2. Interface Name: is the name of the interface created, generally used to reflect the actual meaning of the interface of the English noun expression.
3. The data members in the interface are public static final decorated by default, that is, the data members in the interface are global static constants and must be assigned constant initial values when defined.
4. All methods in the interface are public abstract.
5. Interfaces can inherit only interfaces and cannot inherit classes, but multiple inheritance is possible through an interface.
6. Interfaces have no constructor methods, they cannot be instantiated directly, but allow variables with interface types.
2.2 Implementation of the interface
In Java, using the Implements keyword to implement an interface, the syntax is as follows:
public class class name [extends parent class] Implements interface list {class body;}
3. The similarities and differences between abstract classes and excuses
Same point: 1. Neither can be instantiated
2. Can be applied to polymorphic
different points: 1. Different design concepts are reflected
2. The interface supports multiple inheritance, and abstract classes cannot implement multiple inheritance.
3. Interfaces can only define abstract rules; Abstract classes can define rules and may also provide implemented members.
4. An interface is a set of behavior specifications; An abstract class is an incomplete class that focuses on the concept of a family.
5. Interfaces can be used to support callbacks, and abstract classes cannot implement callbacks because inheritance is not supported.
6. Interfaces only contain signatures for methods, properties, indexers, events, but cannot define fields and methods that contain implementations; Abstract classes can define fields, properties, and methods that contain implementations.
7. Interfaces can be used for value types and reference types, and abstract classes can only be used for reference types.
Java abstract classes and interfaces