ARTICLE

Using Abstract Class in C#

Posted by Vithal Wadje Articles | C# Tutorials November 27, 2012
In this article we learn about abstract classes in C# using a console appication example
Reader Level:

Background

What is an abstract class? This type of question is often asked by interviewers, so due to that I have decided to write this article to help job seekers and anyone who wants to learn about abstract classes. So let us start from the basics so beginners can also understand.
 
What is Abstract Class ?
 
If the class is created for the purpose of providing common fields and members to all subclasses then this type of class is called an abstract class.
 
Syntax of creating an abstract class
 
An abstract class is created in C# using the abstract keyword.
 
Example

abstract public class Pen

Use of Abstract Class

 
There may sometimes be a situation where it is not possible to define a method in a base class and instead every derived class must override that method. In this situation abstract classes or methods are used.
 
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.
 
Some of the key points of abstract classes are:
  • A class may inherit only one abstract class.
  • Members of an abstract class may have any access modifier.
  • Abstract class methods may OR may not have an implementation.
  • Such a type of class cannot be instantiated but it allows the other classes to inherit from it, in other words from an abstract class.
  • Abstract classes allow definition of fields and constants.
Now let us see the abstract class with an example. Use the following procedure to create a console application:
  1. Open Visual Studio from Start - - All programs -- Microsoft Visual Studio.
  2. Then go to to "File" -> "New" -> "Project..." then select Visual C# -> Windows -> Console application.
  3. After that specify the name such as abstract class  or whatever name you wish and the location of the project and click on the OK button. The new project is created.
Add the namespace at the top of the program.cs class file, as in:
 
using System;

And use the following code in the program.cs class file:

 abstract.png
 
Now run the application. The following output will be displayed:
 
ouput.png
 
Summary
 
In the preceding article ,I have briefly explained abstract classes to make them understandable to beginners and newcomers. If you have any suggestion regarding this article then please contact me.

Login to add your contents and source code to this article
post comment
     

thank you charinda nanayakkara sir,it my pleasure

Posted by Vithal Wadje Dec 25, 2012

Thank you sarath kumar sir

Posted by Vithal Wadje Dec 25, 2012

nice simple article.thanx.

Posted by charinda nanayakkara Dec 25, 2012

Hi thanks for your article .. But i am having doubt . How the abstract class will work in asp.net?? Whatever the class we can create object for that directly know.. Whether it will improve in performance???

Posted by sarath kumar Dec 21, 2012

Thank you akhil sir for reading my article, this is a very good question now i am preparing article on that since last few days once it complted i will personally inform you. if you have suggestion regarding my current artilce then contact me i will improve it

Posted by Vithal Wadje Dec 10, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter