Java programming 68 -- abstract classes and interfaces (1)
Chen yuefeng
From:Http://blog.csdn.net/mailbomb
8.9 abstract classes and interfaces
In actual projects, the code of the entire project can generally be divided into Structure Code and logic code. Just like when building a house, you need to first build the structure of the whole House, and then refine the other structures related to the House. Also, when creating a car, you need to first create a car frame, then install accessories and beautify the system. The implementation of Program projects follows the same principle.
In project design, a basic principle is "separation of design and implementation ". That is to say, the separation of the Structure Code and the logic code is like focusing only on the relevant parameters of the car when designing the car, rather than worrying too much about how to implement these requirements. When designing a program, we first design the project structure, instead of having to have too many relationships with the code of each logic.
The process control knowledge introduced earlier mainly solves the logic code writing, while the class and object knowledge mainly solves the structure code writing. Then there is another major question: how to design the structure code? This requires the knowledge of the abstract classes and interfaces described below.
8.9.1 abstract class
Abstract class refers to the class modified with the abstract keyword, that is, the abstract keyword is added when a class is declared. Abstract classes are special classes. Other classes without the abstract keyword are generally called Entity classes. For example:
Public abstract class {
Public (){}
}
Abstract method is a method that uses abstract keywords to modify. Abstract methods are a special method. Other methods without abstract keyword modification are generally called Entity methods.
Public abstract void test ();
Abstract classes are different from entity classes in the following two ways:
L abstract classes cannot use their own constructor to create objects (the syntax is not allowed)
For example, the following syntax is incorrect:
A A = new ();
However, the abstract class can declare objects. For example, the following code is correct:
A;
A A1, A2;
Only declared objects are null by default. They cannot call non-static attributes and non-static methods.
Note: abstract classes can be used to create objects using subclass constructor methods.
L an abstract class can contain any (0, 1, or more) abstract methods.
An abstract class can contain abstract methods or abstract methods. There is no limit on the number of contained methods. The entity class cannot contain abstract methods.
In an abstract class, you can include constructor, attribute, and object method like an object class. This is the same as a general class.
Abstract methods are different from entity methods in the following aspects:
L no method body for abstract methods
That is to say, when declaring an abstract method, the method body {} cannot be written, but the method can only end with a semicolon. The following is a comparison between object methods and abstract method declarations:
Abstract method declaration:
Public abstract void test (int );
Object method declaration:
Public void test (int ){
Method body
}
L The class of the abstract method must be an abstract class.
That is to say, if the abstract method is declared inside a class, the class must be an abstract class. (Note: abstract methods can also appear in the interface, which will be introduced later ).
In this way, if the inherited class is an abstract class and the abstract class also contains an abstract method, the subclass must be declared as an abstract class; otherwise, a syntax error occurs. If the subclass needs to be made into an object class, it must overwrite all the inherited abstract methods. This is the core syntax function of the abstract class-force subclass to overwrite some methods.
After introducing so many abstract classes and abstract methods, what are the purposes of abstract classes?
Abstract classes are mainly used in two ways:
L It is strictly prohibited to directly create objects of this class.
If all the methods contained in a class are static, you can declare the class as abstract to avoid misuse by other programmers, so that other programmers can only use the class name. the method name calls the corresponding method, rather than the object name. call the method name. Such a class, such as the math class in the API
Note: when used with the final keyword, the class must be inherited to achieve better results.
L force subclass override abstract Method
In this way, all sub-classes can be consistent in the method declaration, and the functions of the method must be logically consistent. For example, when designing a category in a game, a category of monsters and related sub-classes are designed. each category of monsters has a moving method, but the moving rules of each type of monsters are different, in this way, the declaration of the moving method of each monster class is consistent, making it easy to call. You can refer to the introduction in the polymorphism section to learn more about unified calling.
This is the main purpose of an abstract class. Just like in the real society, various banking outlets maintain a uniform decoration style, and various fast food restaurants (such as KFC and McDonald's) maintain a uniform decoration and even flavor, so as to facilitate the identification in life. The method with the same functions (but different internal implementation rules) in the class with inheritance relationships is declared as the same, facilitating the use of polymorphism.
So when should I use abstract classes during design? For this question, see the usage of the abstract class. The abstract class is introduced so much. The following describes the interface knowledge and compares the abstract class with the interface.