codecandle logo




 

C# - Basics - Classes - Inheritance - Abstract Classes


Related: C# Tutorial

Date: 06.10.2011

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.


namespace NS_abstractClassExampleApp
{
//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.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace NS_HRapplication
{
//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()
{
Console.WriteLine("1000 euro...");
}
}
class CL_x
{
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...
o_anEmployee.FN_displaySalary();

Console.ReadLine();
}
}
}



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.


Application output:
C# - Basics - Classes - Inheritance - Abstract Classes


Area: articles

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)
  • comment icon
  • Comments Section
 
  • 11.04.2014 19:42:54 post by qStkC

fC8gO

  • 23.03.2014 17:07:10 post by eDAVA

2M9cz

  • 21.03.2014 11:33:48 post by i3pq6

2hcqlz

  • 18.03.2014 16:03:07 post by aYSZf

Adq1k

  • 17.03.2014 12:58:26 post by kBA6L

VCaf5

  • 12.03.2014 15:16:31 post by FE9ZGY

hwezgp

  • 28.02.2014 12:34:24 post by MNcggx

XK4SSx

  • 13.02.2014 11:41:41 post by jvmhdV

kmLUFi

  • 16.01.2014 07:52:35 post by kl

jkk

  • 05.04.2013 10:10:41 post by Alias Name

Comment...

  • 29.10.2012 13:22:14 post by Alias Name

Comment...ertyu6i

  • Post your Comment:


Chat section


[ Copy this | Start New | Full Size ]

CodeCandle

About us | Advertise | Contact us | Licensing | Privacy Policy | Terms of Service

© 2010 CodeCandle. All rights reserved.