Java15-java Grammar Basics (14) Abstract class
I. The role of abstract classes
Three classes have a "Execute task" method, which is defined in each of the three classes, so it is necessary to write code repeatedly, reduce the efficiency of program development, and increase the chances of errors in the program.
Use an abstract class to solve the problem.
Ii. abstract classes and abstract methods
1, abstract class: Abstract class depicts the characteristics of public behavior, but not in the internal implementation of the abstract class of these public behavior, but through the inheritance mechanism by the derived class to achieve concrete public behavior.
2. Abstract method: All member methods that are modified by abstract are abstract methods, and only the methods are declared and there are no concrete implementations of the methods.
3. Syntax:
[Access control modifier] abstract class class name {
[Access control modifier] Return value type method name ([parameter]) {method Body}//Common method
[Access control modifier] Abstract return value type method name ([parameter]); Abstract methods
}
Emphasize:
(1) Abstract classes can have abstract methods, can also have a common method;
(2) The abstract method is only declared, not implemented, and the ordinary method has the concrete realization namely the method body.
III. limitations of abstract classes and abstract methods
1, if a non-abstract class inherits the abstract class, it must overwrite and implement all the abstract methods of the parent class;
2, if an abstract class inherits the abstract class, it can not overwrite the abstract method of the parent class, only need to declare the abstract method of the base class again as abstract;
3. Abstract classes can contain both abstract and non-abstract methods.
4, if there is an abstract method in a class, then this class must be declared as an abstract class;
5, can not use private to modify the abstract method, because the private modification of the method is not visible, can not be covered in subclasses of sub-class;
6. You cannot use final to modify an abstract class, because the final decorated class cannot be inherited, so the definition of the abstract class is meaningless.
7, abstract class can have a construction method, but the abstract class cannot be directly instantiated with the construction method.
Iv. Summary
1, the role of abstract class design?
2. The relationship between abstract classes and abstract methods?
3, the design of abstract class need to pay attention to the problem?
4. What are the differences between abstract classes and interfaces?
(1) Abstract classes are used for inheritance.
(2) The interface is used to implement
Java15-java Grammar Basics (14) Abstract class