There is a special base class in the C # language, which is an abstract class. Sometimes, a base class is not associated with a specific thing, but rather simply an abstract concept that provides a common interface for its derived classes. For this reason, the concepts of abstract classes and abstract methods are introduced in C #.
Abstract class overview
Abstract classes provide multiple derived classes that share a common definition of a base class that can provide either an abstract method or a non-abstract method. Abstract classes cannot be instantiated, and they must be implemented by derived classes to implement their abstract methods, so the new keyword cannot be used for abstract classes and cannot be sealed. If a derived class does not implement all of the abstract methods, the derived class must also be declared as an abstract class. In addition, implementing an abstract method is implemented by the override method.
Abstract classes Use the abstract modifier, which provides a few rules for the use of abstractions:
1 abstract classes can only be used as base classes for other classes, cannot be instantiated directly, and cannot use the new operator for abstract classes. Abstract classes, if they contain abstract variables or values, are either null types or contain references to instances of non-abstract classes.
2 abstract classes allow the inclusion of abstract members, although this is not required.
3 Abstract classes cannot be sealed at the same time.
4 If a non-abstract class derives from an abstract class, it must be overloaded to implement all inherited abstract members.
The abstract keyword is used in C # when declaring an abstraction class, and the syntax format is:
Access modifier abstract class class name: base class or interface
{
Class members;
}
When declaring an abstract class, other than the abstract keyword, the class keyword, and the class name, are optional.
Abstract methods Overview
Because abstract classes themselves express abstract concepts, many of the methods in a class do not necessarily have to be implemented in a specific way, but instead allow an interface to be overloaded as a derived class.
If you add an abstract modifier to a method declaration, this method is called an abstract method. If a method is declared also abstract, the method defaults to a virtual method. In fact, the abstract method is a new virtual method, it does not provide a concrete method to implement the program, rather than the virtual derived class requires overloading for the inherited virtual method to provide its own implementation, and the abstract method does not contain the concrete implementation content, so the method declaration of the execution body only a semicolon ";". The user can only declare abstract methods in an abstract class. For an abstract method, you can no longer use the static or virtual modifier, and the method is not eng implemented by any executable program, even if only a pair of curly braces in the middle of a semicolon ";" Are not allowed to appear, only need to give the prototype of the method can be.
There are a few things to note when declaring an abstract method:
1 abstract methods must be declared in an abstract class.
2 You cannot use the virtual,static and private modifiers when declaring an abstract method.
Abstract method declarations introduce a new method, but do not provide an implementation of the method, because the abstract method does not provide any actual implementation, so the method body of the abstract method contains only a semicolon. When deriving a non-abstract class from an abstract class, you need to override the abstract method in the fly abstract class to provide an implementation of the concrete abstract method, using the Override keyword When overriding the abstract method.
Here's a two example:
<span style= "font-size:18px;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace qq{public abstract class a//abstract classes {public abstract void Add (int A, int b);//Abstract method} public CLA SS b:a//derived class B {public override void Add (int A, int B)//override abstract method {int sum = A + B; Console.WriteLine (sum); }} public class c:a//derived class C {public override void Add (int A, int b)//override abstract method {int sum = System.Math.Abs (A + b);//The absolute value of the sum of two numbers Console.WriteLine (sums); }} class Program {static void Main (string[] args) {b p = new B (); P.add (1,-2); c q = new C (); Q.add (1,-2); Console.ReadLine (); }}}</span>
It is easy to know that the output results are:-1 and 1
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; namespace qq{public class Abtext {public virtual void output () { Console.WriteLine ("This is a virtual method"); } } Abstract public class Class1:abtext {public abstract override void output (); } public class Class2:class1 {public override void output () { Console.WriteLine ("This is a new method"); } } Class program { static void Main (string[] args) { Class2 c1 = new Class2 (); C1.output (); Console.ReadLine ();}}} </span>
It is easy to know the result is: This is a new method
Abstract class of C #