Please enable Javascript to correctly display the contents on Dot Net Tricks!

A Deep Dive into C# Abstract Class

  Author : Shailendra Chauhan
Updated On : 05 Nov 2013
Total Views : 123,315   
Support : All C#
 

Abstract class is a special type of class which cannot be instantiated and acts as a base class for other classes. Abstract class members marked as abstract must be implemented by derived classes.

The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.

For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class. In C#, System.IO.FileStream is an implementation of the System.IO.Stream abstract class.

abstract class ShapesClass
{
 abstract public int Area();
}
class Square : ShapesClass
{
 int side = 0;

 public Square(int n)
 {
 side = n;
 }
 // Override Area method 
 public override int Area()
 {
 return side * side;
 }
}

class Rectangle : ShapesClass
{
 int length = 0, width=0;

 public Rectangle (int length, int width)
 {
 this.length = length;
 this.width = width;
 }
 // Override Area method 
 public override int Area()
 {
 return length * width;
 }
}

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.

Common design guidelines for Abstract Class

  1. Don't define public constructors within abstract class. Since abstract class cannot be instantiate and constructors with public access modifiers provides visibility to the classes which can be instantiated.

  2. Define a protected or an internal constructor within an abstract class. Since a protected constructor allows the base class to do its own initialization when sub-classes are created and an internal constructor can be used to limit concrete implementations of the abstract class to the assembly which contains that class.

When to use

  1. Need to create multiple versions of your component since versioning is not a problem with abstract class. You can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change.

  2. Need to to provide default behaviors as well as common behaviors that multiple derived classes can share and override.

What do you think?

I hope, now you have got everything about abstract class. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (09:30 AM-11:00 AM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+