A class that contains one or more pure virtual functions is called an abstract class. Use the keyword Abstract declaration.
Abstract classes cannot be initialized, only partial implementations are provided. Can be inherited, classes that inherit abstract classes must implement virtual methods in the abstract class. Abstract classes can only be used by interfaces or as base classes of other classes.
Abstract classes can be used for classes, properties, methods, indexes, and events. Use the abstract declaration to indicate that you want to use as a base class for another class, or to be included in an abstract class that must be implemented by its derived class.
Abstract class shapeclass{ abstractpublicint area ();} class square:shapeclass{intpublicoverrideint return x*y;}}
Example: An abstract class that contains non-abstract methods.
1 Abstract classMyabt2 {3 Public voidMyMethod () {4Console.WriteLine ("This is a non-abstract method");5 }6 }7 classMyclass:myabt8 {9 Ten } One classmyclient{ A Public Static voidMain () { -MyClass m=NewMyClass (); - M.mymethod (); the } -}
An abstract class can contain both abstract and non-abstract methods, but when inherited, derived classes must implement all of the base class abstract methods.
Abstract methods are methods that do not have a method body.
Abstract classmyabt{ Public voidNonabtmethod () {Console.WriteLine ("This is a non-abstract class method");} Abstract Public voidAbtmethod ();}classmyclass:myabt{ Public Override voidAbtmethod () {Console.WriteLine ("This is an abstract approach .");}}classmyclient { Public Static voidMain () {MyClass m=NewMyClass (); M.nonabtmethod (); M.abtmethod ();}}
You can also avoid implementations of all or specific abstract methods by declaring derived classes as well as abstractions. However, derived classes of derived classes are also implementing abstract methods.
Partial implementations of abstract classes.
Abstract classmyabt{Abstract Public voidMyMethod1 (); Abstract Public voidMyMethod2 ();}Abstract classmyclass1:myabt{ Public Override voidMyMethod1 () {Console.WriteLine ("implemented the first abstract method"); }}classmyclass:myclass1{ Public Override voidMyMethod2 () {Console.WriteLine ("implements the second abstract method");}}
Classvoid Main () {MyClass m=new MyClass (); M.mymethod1 (); M.mymethod2 ();}}
In C #, an abstract class can inherit another non-abstract class, and, in addition, inherits the method of the base class, adding a new
Abstract and non-abstract methods are feasible.
classMyClass1//Non-abstract class { Public voidMethod1 () {Console.WriteLine ("Method of a non-abstract class"); } } Abstract classMyabs:myclass1//Inherits from an Non-abstract class { Public Abstract voidAbMethod1 (); } classMyclass:myabs//must implement base class abstract methods { Public Override voidAbMethod1 () {Console.WriteLine ("abstarct method #1 of MyClass"); } } classmyclient { Public Static voidMain () {MyClass MC=NewMyClass (); Mc. Method1 (); Mc. AbMethod1 (); } }
An abstract class can also be implemented from an interface, in which case we must provide the method body for all the methods that come from the interface
InterfaceIInterface {voidMethod1 (); } Abstract classMyabs:iinterface { Public voidMethod1 () {Console.WriteLine ("Method implemented from the IInterface"); } } classMyclass:myabs//must implement base class abstract method { } classmyclient { Public Static voidMain () {MyClass MC=NewMyClass (); Mc. Method1 (); } }
Abstract methods have the following characteristics:
1. An abstract method can be thought of as a virtual function.
2. The declaration of an abstract method can only be in an abstract class.
3. Because the abstract method declaration only provides a non-implementation way, there is no method body
4. Method body implementation of the Overwrite method is provided, the overridden method is a member of a non-abstract class.
5. The behavior of abstract attributes is similar to abstract methods, except for different declarative forms.
6. Using abstract in a static property is an error.
* An abstract property can be implemented using override with a derived class.
Abstract classes have the following characteristics:
1. Abstract classes cannot be instantiated.
2. Abstract classes can contain abstract methods and accessors
3. It is not possible to modify an abstract class with a seal (sealed), which means that the class cannot be inherited, which violates the principle that the abstract class is inherited.
4. A non-abstract class derived from an abstract class must include all the inherited abstract methods and accessors of the implementation
5. The use of the abstract keyword in methods and properties implies that the implementation contains them.
C # Abstract classes and abstract methods