C# - Basics - Classes - Inheritance - Abstract Classes
Related: C# Tutorial
Date: Ekim 6, 2011
Area: C# Tutorials
Abstract Classes in C#
This topic contains and tries to cover following subjects:
- Explanation of Abstract Classes in C#
- Syntax of Abstract Class declaration and implementation in C#
- An example of Abstract Class in C#
Articles provides solution to following questions and issues:
- How to declare an Abstract Class?
- How to implement an Abstract Class in derived Class?
- Why and where Abstract Classes are used in practice?
- How to prevent a Class to be instantiated?
Explanation of Abstract Classes in C#
Abstract Classes allows programmers to create their Classes with locking the Class, and restricting an object from it to be instantiated-created. In C#, Abstract Classes can not be instantiated. The functions of Abstract Class, and members are declared only. Functions does not contain an implementation. One of the idea behind the Abstract Class is to design template Class. It is some sort of outlining the Class as template, and in future to use it as base Class for derived Classes to inherit it.
Syntax of Abstract Class Creation in C#
Following syntax is used to create an abstract Class. Abstract Classes only contain declarations. Methods of Abstract Classes does not contain code in body, and ends with ";". Further in derived Class which inherits this abstract Class as base Class, method of abstract Class is created with adding "override" keyword preceding its return time and after access modifier. And function body-code is provided in derived Class.
//declare a base Class...
abstract class CL_aBaseClass
//declaring a method without method body implementation...
public abstract void FN_aFunction();
//create a derived Class and inherit base Abstract Class...
class CL_aDerivedClass : CL_aBaseClass
public override void FN_aFunction()
//Abstract Class method is implemented here with overriding it...
Example of Abstract keyword in C#
Following example indicates how to declare and implement an Abstract Class in Visual Studio 2008 console application.
//declare a base Class...
abstract class CL_employees
public abstract void FN_displaySalary();
//create a derived Class and inherit base Class...
class CL_tehnicians : CL_employees
//override abstract Class function...
public override void FN_displaySalary()
static void Main(string  args)
//create an instance of derived Clas...
CL_tehnicians o_anEmployee = new CL_tehnicians();
//function of derived Class is called by its instance...
In preceding example, we created an Abstract Class. Further added a derived Class and inherited a method with overriding it. Finally, console application calls the method with instantiating the derived Class.
Reference resources: http://msdn.microsoft.com/en-us/library/k535acbf%28v=VS.71%29.aspx
Tags: C# abstract Classes
Catagory layer 1: Programming
Catagory layer 2: Languages
Code layer 1: Languages
Code layer 2: csharp
Code layer 3: Basics
Code layer 4: classes
Code layer 5: abstract-classes
Reader layer 1: beginner
Sector layer 1: C# programmer
Fuzzy layer 1: How to create an abstract Class in C#
Fuzzy layer 2: how to lock a Class to prevent instantiating object from it in C#
Nodes: Languages\C# (1)