1. Abstract class
In object-oriented concepts, all objects are depicted by classes, but conversely, not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class.
Abstract classes In addition to the object cannot be instantiated, other functions of the class still exist, member variables, member methods and construction methods are accessed in the same way as the normal class.
Because an abstract class cannot instantiate an object, an abstract class must be inherited in order to be used . For this reason, it is often decided at the design stage to not design abstract classes.
The parent class contains a common method for the collection of subclasses, but because the parent class itself is abstract, these methods cannot be used.
In Java, an abstract class represents an inheritance relationship in which a class can inherit only one abstract class, while a class may implement multiple interfaces.
2. Abstract methods
If you want to design a class that contains a special member method whose implementation is determined by its subclasses , you can declare the method as an abstract method in the parent class.
The abstract keyword can also be used to declare an abstraction method, which contains only one method name and no method body.
Abstract methods are not defined, followed by a semicolon, not a curly brace, directly following the method name.
Declaring an abstract method can result in the following two results:
- If a class contains an abstract method, the class must be an abstract class .
- any subclass must override the abstract method of the parent class, or declare itself an abstract class .
Subclasses of inherited abstract methods must override this method. Otherwise, the subclass must also be declared as an abstract class. Eventually, a subclass must implement the abstract method, otherwise, from the original parent class to the final subclass, it cannot be used to instantiate the object.
3, abstract class summary provisions
1. Abstract classes can not be instantiated (beginners are prone to make mistakes), if instantiated, will error, compile cannot pass. Only non-abstract subclasses of abstract classes can create objects.
2. Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.
3. Abstract methods in abstract classes are only declarations, do not contain method bodies, that is, the concrete implementation of the method is the specific function of the method.
4. A constructor method, a class method (a static decorated method) cannot be declared as an abstract method .
5. Subclasses of abstract classes must give concrete implementations of abstract methods in abstract classes (that is, overrides), unless the subclass is also an abstract class.
Java Abstract class