09 July 2014

Abstract Class

In dynamic polymorphism (over riding) 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,

  1. Abstract Classes

  2. Virtual Function


A class can be consumed from other classes in two different approaches,

  1. Inheritance (Inherit & Consume).

  2. By creating the object and consume.


Abstract class: The class under which we declared abstract methods is known as abstract class and should be declared with 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.



  1. 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.

  2. 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.

  3. The method overriding re-implemented/overriding the method is optional in virtual methods. Where as in abstract methods implementing/overriding the method is mandatory.


abs n vir

No comments: