In dynamic polymorphism the object of class reused with polymorphism method which has called only in the runtime. The decision about function execution is made at run time. Method overloading, method overriding, hiding comes under this approach.
The mechanism of linking a function with an object at run time is called dynamic or late binding.
C# uses two approaches to implement dynamic polymorphism,
- Abstract Classes
- Virtual Function
A class can be consumed from other classes in two different approaches,
- Inheritance (Inherit & Consume).
- By creating the object and consume.
Abstract classes: – The class under which we declared this abstract method is known as an abstract class and should be declared abstract modifier.
An abstract class can contain abstract members as well as non-abstract members.
A Method without any method body is known as an Abstract method. It contains only the declaration of the method, it should be declared by using the abstract modifier. This incomplete (abstract methods) must be implemented in a derived class.
An abstract class cannot be instantiated directly. An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited. If it is so, it is useless.
An abstract method is implicitly a virtual method. This is accomplished by adding the keyword abstract before the return type of the method. An abstract member cannot be static.
The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.
Use abstract classes when you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes. As it simplifies versioning, this is the practice used by the Microsoft team which developed the Base Class Library. (COM was designed around interfaces.)
So, abstract class defines a common base class for a family of types with a default behavior
For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.
An Abstract class can
- Had instance variables (like constants and fields), constructors and destructor.
- Can inherit from another abstract class or another interface.
An Abstract class cannot
- Inherited by structures.
- Support multiple inheritances.
- The concept of abstract method is nearly related with the concept of method overriding. Where in overriding parent class declared a method as virtual and child class re-implements that method by using the override keyword.
- In case of abstract method parent class method is abstract which has to be implemented by the child class by using the override keyword only.
- The method overriding re-implemented/overriding the method is optional in virtual methods. Where as in abstract methods implementing/overriding the method is mandatory.