Abstract concept
Abstract: general, vague, not understand, not understand
Abstract class features:
1. If a class has only method declarations but no method body (no implementation). So this method is an abstract method. The class in which the method resides must be abstract, so they all need to be abstract.
2. Can abstract classes be instantiated? Abstract classes cannot be instantiated (creating objects) because methods are not implemented in abstract methods.
3. An abstract class can be instantiated only after its subclasses have overridden (overridden) all the methods in the abstract class. Subclasses must implement all abstract methods
Details of the abstract class:
1. Are there constructors in abstract classes? Yes, used to create a subclass object to initialize the parent class.
2. Can abstract classes not define abstract methods? You can.
3. Which keywords cannot coexist with the abstract keyword? private sealedstatic
What’s the difference between an abstract class and an ordinary class?
Similarities:
- Both abstract and ordinary classes can define any member (method, attribute, field, constructor) inside the class that describes something.
Difference:
-
Common classes have enough information to describe things, while abstract classes may not have enough information to describe food
-
Abstract methods cannot be defined in ordinary classes, only non-abstract methods can be defined. Abstract classes can be defined in either way.
-
Ordinary classes can be instantiated: abstract classes cannot be instantiated
Must an abstract class be a superclass?
Yes, an abstract class can only instantiate a subclass object if it is inherited by a subclass and overrides all of its methods
abstract classAnimal
{
public int a = 3;
public abstract void Eat();
}
class Dog: Animal // Subclasses inherit abstract classes
{
publicoverridevoidEat()// re-implement Eat
{
Console.WriteLine("Bone gnawing"+ a); }}class Demo
{
static viodMain(string[]args)
{
newDog().Eat(); // only instantiated subclasses can call methods}}Copy the code
Abstract class features:
Abstract classes and methods must be qualified with the abstract keyword.
Abstract methods have only method declarations, no method bodies, and are defined in abstract classes. Format: modifier abstract Return value type Function name (argument list).
Abstract classes cannot be instantiated, that is, objects cannot be created with new. Because abstract class is extracted from concrete things, itself is not concrete, there is no corresponding instance. For example: Canids are an abstract concept; there really are wolves and dogs. An abstract class is instantiated by its subclass, which needs to override (overwrite) all the abstract methods in the abstract class before it can create an object, otherwise the subclass is also abstract. Abstract classes can have non-abstract methods inside them.
The difference between an abstract class and an ordinary class:
-
Abstract class declarations are defined using the abstract keyword, while ordinary classes are not.
-
An abstract method in an abstract class cannot have a method body, only a method declaration.
-
When an abstract class is inherited, a subclass must reimplement all of its abstract methods, whereas ordinary classes do not.
-
Abstract classes most of the time have abstract methods, and ordinary classes definitely do not have abstract methods.
Usage Scenarios:
-
When you don’t know how to implement a method in a parent class, you can think about writing the parent class as an abstract class, and the method as an abstract method,
-
If a method in a parent class has a default implementation and the parent class needs to be instantiated, consider defining the parent class as a normal class with virtual methods to implement polymorphism
-
A class can be defined as abstract if the methods in the parent class do not have a default implementation and the parent class does not need to be instantiated