Your Ad Here

Wednesday, April 22, 2009

Abstract Class

Where can we use abstract modifier?
The abstract modifier is used with following:
1) classes
2) methods
3) properties
4) indexers
5) events

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

public Square(int n)
{
side = n;
}
// Area method is required to avoid
// a compile-time error.
public override int Area()
{
return side * side;
}

static void Main()
{
Square sq = new Square(12);
Console.WriteLine("Area of the square = {0}", sq.Area());
}

interface I
{
void M();
}
abstract class C : I
{
public abstract void M();
}

}

Benefits for using Abstract class.
The real benefit of using abstract classes is that the extended classes
can implement the abstract methods in their own way.

No comments:

Post a Comment