Use the keyword abstract in C # To define abstract classes and abstract methods.
Classes that cannot be initialized are called abstract classes. They only provide partial implementation, but the other class can inherit it and create them.
.
"An abstract class that contains one or more pure virtual functions cannot be instantiated.
An abstract class can only be used by interfaces and as the base classes of other classes. "-C ++ Programming Language by Stroustrup Chapter13.2
Abstract classes can be used for classes, methods, attributes, indexers, and events. abstract indicates that the class tends to be used as the base class of other classes in a class declaration.
A member is marked as abstract or included in an abstract class, which must be implemented by its derived class.
Abstract class ShapesClass
{
Abstract public int Area ();
}
Class Square: ShapesClass
{
Int x, y;
// Not providing an Area method results
// In a compile-time error.
Public override int Area ()
{
Return x * y;
}
}
For example, an abstract class containing non-Abstract METHODS:
Abstract class MyAbs
{
Public void NonAbMethod ()
{
Console. WriteLine ("Non-Abstract Method ");
}
}
Class MyClass: MyAbs
{
}
Class MyClient
{
Public static void Main ()
{
// MyAbs mb = new MyAbs (); // not possible to create an instance
MyClass mc = new MyClass ();
Mc. NonAbMethod (); // Displays 'non-Abstract Method'
}
}
An abstract class can contain abstract and non-Abstract METHODS. When a class inherits from an abstract class, this derived class must implement all
.
An abstract method is a method without a method body.
Abstract class MyAbs
{
Public void NonAbMethod ()
{
Console. WriteLine ("Non-Abstract Method ");
}
Public abstract void AbMethod (); // An abstract method
}
Class MyClass: MyAbs // must implement base class abstract methods
{
Public override void AbMethod ()
{
Console. WriteLine ("Abstarct method ");
}
}
Class MyClient
{
Public static void Main ()
{
MyClass mc = new MyClass ();
Mc. NonAbMethod ();
Mc. AbMethod ();
}
}
However, by declaring a derived class as an abstraction, we can avoid the implementation of all or specific virtual methods,
This is part of the abstract class implementation.
Abstract class MyAbs
{
Public abstract void AbMethod1 ();
Public abstract void AbMethod2 ();
}
// Not necessary to implement all abstract methods
// Partial implementation is possible
Abstract class MyClass1: MyAbs
{
Public override void AbMethod1 ()
{
Console. WriteLine ("Abstarct method #1 ");
}
}
Class MyClass: MyClass1
{
Public override void AbMethod2 ()
{
Console. WriteLine ("Abstarct method #2 ");
}
}
Class MyClient
{
Public static void Main ()
{
MyClass mc = new MyClass ();
Mc. AbMethod1 ();
Mc. AbMethod2 ();
}
}
In C #, an abstract class can inherit from another non-abstract class. In addition, it inherits the methods of the base class and adds a new
Abstract and non-abstract methods are feasible.
Class MyClass1 // Non-Abstract class
{
Public void Method1 ()
{
Console. WriteLine ("Method of a non-abstract class ");
}
}
Abstract class MyAbs: MyClass1 // Inherits from an non-abstract class
{
Public abstract void AbMethod1 ();
}
Class MyClass: MyAbs // must implement base class abstract methods
{
Public override void AbMethod1 ()
{
Console. WriteLine ("Abstarct method #1 of MyClass ");
}
}
Class MyClient
{
Public static void Main ()
{
MyClass mc = new MyClass ();
Mc. Method1 ();
Mc. AbMethod1 ();
}
}
An abstract class can also be implemented from an interface. In this case, we must provide the method body for all methods. These methods are from interfaces.
Interface IInterface
{
Void Method1 ();
}
Abstract class MyAbs: IInterface
{
Public void Method1 ()
{
Console. WriteLine ("Method implemented from the IInterface ");
}
}
Class MyClass: MyAbs // must implement base class abstract method
{
}
Class MyClient
{
Public static void Main ()
{
MyClass mc = new MyClass ();
Mc. Method1 ();
}
}
We cannot use abstract keywords and sealed together in C #, because a sealed class cannot be abstracted.
Abstract class MyAbs
{
Public abstract void AbMethod1 ();
Public abstract void AbMethod2 ();
}
Class MyClass1: MyAbs
{
Public override void AbMethod1 ()
{
Console. WriteLine ("Abstarct method #1 of MyClass1 ");
}
Public override void AbMethod2 ()
{
Console. WriteLine ("Abstarct method #2 of MyClass1 ");
}
}
Class MyClient
{
Public static void Main ()
{
MyAbs ma1 = new MyClass1 (); // Polymorphism
Ma1.AbMethod1 ();
Ma1.AbMethod2 ();
}
}
Abstract methods have the following features:
1. An abstract method can be considered as a virtual function.
2. The declaration of abstract methods can only be in abstract classes.
3. Because the abstract method declaration only provides one unimplemented method, there is no method body
4. The method body implementation is provided by the overwriting method. The overwriting method is a member of a non-abstract class.
5. The behavior of abstract attributes is similar to that of abstract methods, except for different declarations.
6. using abstract in a static attribute is an error.
* An abstract attribute can be implemented by using override in a derived class.
For abstract classes:
An abstract class must be implemented for all interface members.
An abstract class used to implement interfaces may arrange interface methods to abstract methods. For example
Interface I
{
Void M ();
}
Abstract class C: I
{
Public abstract void M ();
}
Abstract classes have the following features:
1. abstract classes cannot be instantiated.
2. abstract classes can contain abstract methods and accessors
3. the abstract class cannot be modified by sealed, which means that the class cannot be inherited, which violates the principle of being inherited.
4. A non-abstract class derived from an abstract class must include all the inherited Abstract methods and accessors.
5. Using abstract keywords in methods and attributes means that they are included.
To be continued ......
From longtai