Abstract modifier, the difference between a specific class and an abstract class, abstract Modifier
Abstract Modifier
Abstract: A class can be modified to indicate an abstract class. Note: the unique difference between an abstract class and a specific class: A class cannot create objects, but a specific class can create objects.
1. The specific classes have constructor methods, and the abstract classes also have constructor methods.
2. A specific class can have instance attributes, and an abstract class can also have instance attributes.
3. The specific class can have instance methods, and the abstract class can also have instance methods.
4. A specific class can inherit another specific class, and an abstract class can inherit another specific class.
Abstract: A method can be modified to indicate that this is an abstract method, and there is a declaration of the method, which is not implemented.
Abstract classes are specially used for inheritance.
When a class inherits the abstract class, if all the abstract methods in the abstract class are implemented, the class is a specific class.
If a class inherits an abstract class and only implements the abstract methods in the abstract class, the class must be declared as an abstract class.
Abstract c #?
Abstract classes are used as base classes
Cannot be instantiated
It is used to derive other non-abstract classes.
The interface mainly implements Multi-inheritance.
Abstract modifier is used to indicate that the modified class is incomplete and can only be used as the base class. Abstract classes and non-abstract classes are different in the following aspects:
• Abstract classes cannot be directly instantiated, and the use of the new operator for abstract classes will cause compilation errors. Although some types of variables and values during compilation can be abstract, such variables and values must be null, or reference to instances that contain non-abstract classes (this non-abstract class is derived from the abstract type ).
• Allowed (but not required) abstract classes include abstract members.
• Abstract classes cannot be sealed.
When a non-abstract class is derived from an abstract class, these non-abstract classes must specifically implement all the abstract members inherited to override those abstract members.
In the following example
Abstract class
{
Public abstract void F ();
}
Abstract class B:
{
Public void G (){}
}
Class C: B
{
Public override void F (){
// Actual implementation of F
}
}
Abstract class A introduces abstract method F. Class B introduces another method G, but because it does not provide the Implementation of F, B must also be declared as an abstract class. Class C overrides F and provides a specific implementation. Since C does not have abstract members, C can (but is not required) Be non-abstract.
What is the difference between an abstract class and a non-abstract class?
Abstract classes have the following features:
Abstract classes cannot be instantiated.
Abstract classes can contain abstract methods and abstract accessors.
The abstract class cannot be modified with the sealed modifier, which means the class cannot be inherited.
A non-abstract class derived from an abstract class must include all the inherited abstract methods and the actual implementation of the abstract accessors.
Use the abstract modifier in a method or attribute declaration to indicate that this method or attribute does not contain an implementation.
Abstract methods have the following features:
The abstract method is an implicit virtual method.
Only abstract methods can be declared in abstract classes.
Because the abstract method declaration does not provide real implementation, there is no method body. The method declaration ends with a semicolon and there is no braces ({}) after the signature ({}). For example:
Public abstract void MyMethod ();
The implementation is provided by the overriding method, which is a non-abstract class member.
It is incorrect to use the static or virtual modifier in the abstract method declaration.
Except for the differences in the declaration and call syntax, abstract attributes behave the same way as abstract methods.
It is incorrect to use the abstract modifier for static attributes.
In a derived class, you can override abstract inheritance attributes by including the attribute declaration using the override modifier.
Abstract classes must be implemented for all interface members.