Thursday, 26 July 2012

Abstract Classes In C#


Abstract Classes In C#

Abstract classes are one of the essential behaviors provided by .NET. Abstract classes are created if you want to make classes that only represent base classes, and don’t want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'.
So an abstract class means that, no object of this class can be instantiated, but can make derivations of this. The abstract keyword enables you to create classes and class members solely for the purpose of inheritance—to define features of derived, non-abstract classes.
Example:
abstract class absClass1
{
public abstract int AddTwoNumbers(int Num1, int Num2);
public void PrintNumbers()
{
int res = 10;
Console.WriteLine("Result: "+ res);
}
}
class absClass2 : absClass1
{
public override int AddTwoNumbers(int Num1, int Num2)
{
return Num1 + Num2;
}
public static void Main()
{
absClass2 cls2 = new absClass2();
Console.WriteLine(cls2.AddTwoNumbers(30, 30));
cls2.PrintNumbers();V }
}

No comments:

Post a Comment