Tuesday, June 23, 2015

Abstract Class

The purpose of abstract class is to provide default functionality to its sub classes.

When a method is declared as abstract in the base class then every derived class of that class must provide its own definition for that method.

An abstract class can also contain methods with complete implementation, besides abstract methods.
When a class contains at least one abstract method, then the class must be declared as abstract class

It is mandatory to override abstract method in the derived class.
When a class is declared as abstract class, then it is not possible to create an instance for that class.

They are commonly used when you want to define a template for a group of subclasses that share some common implementation code, but you also want to guarantee that the objects of the superclass cannot be created.

Use abstract classes when you are defining behaviour for a class in your class heirarchy that is never going to be used to instantiate an object directly.  

Cannot instantiate abstract class. The important part of an abstract class is that you can never use it separately from a derived class. Therefore in Main you cannot use the new Test() constructor.

Compared to an interface, it adds features and improves performance.

Features of Abstract Class

  1. An abstract class cannot be instantiated.
  2. An abstract class contain abstract members as well as non-abstract members.
  3. 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.
  4. A non-abstract class which is derived from an abstract class must include actual implementations of all the abstract members of parent abstract class.
  5. An abstract class can be inherited from a class and one or more interfaces.
  6. An Abstract class can has access modifiers like private, protected, internal with class members. But abstract members cannot have private access modifier.
  7. An Abstract class can has instance variables (like constants and fields).
  8. An abstract class can has constructors and destructor.
  9. An abstract method is implicitly a virtual method.
  10. Abstract properties behave like abstract methods.
  11. An abstract class cannot be inherited by structures.
  12. An abstract class cannot support multiple inheritance
Reference Link :