Thursday, May 3, 2012

Abstract Classes in C#

In this post I am going to explain about the Abstract classes in the C#.An abstract class is one that cannot be directly instantiated. Instead derived classes must inherit from it and their constructors must be used.To declare a abstarect class we will use the keyword “ Abstract”.

An abstract class is a parent class that allows inheritance but can never be instantiated. Abstract classes contain one or more abstract methods that do not have implementation. Abstract classes allow specialization of inherited classes.
Classes can be declared as abstract. This is accomplished by putting the keyword abstract before the keyword class in the class definition.
For example:
public abstract class A
{
    // Class members here.
}

Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. For example:

public abstract class A
{
    public abstract void DoWork(int i);
}

Abstract classes are one of the essential behaviors provided by .NET. Commonly, you would like 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'.

An abstract class means that, no object of this class can be instantiated, but can make derivations of this.

Abstract class vs. Interface

An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.

Defining an abstract class with abstract members has the same effect to defining an interface.
The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc.

A class can inherit one or more interfaces, but only one abstract class.
Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.

The following program will explain you how to create a abstract class and how to use the abstract class in the derived class.


  abstract class Test
    {
        public int _a;
        
        public abstract void A();
    }

    class Example1 : Test
    {
        public override void A()
        {
            base._a = 10;
            Console.WriteLine("Example1.A");
            base._a++;
            Console.WriteLine("a value =" + base._a);
        }
    }

    class Example2 : Test
    {
        public override void A()
        {
            base._a = 5;
            Console.WriteLine("Example2.A");
            base._a--;

            Console.WriteLine("a value =" + base._a);
        }
    }

    class AbstractEx
    {
        static void Main()
        {
            // Reference Example1 through Test type.
            Test test1 = new Example1();
            test1.A();

            // Reference Example2 through Test type.
            Test test2 = new Example2();
            test2.A();
            Console.ReadKey();
        }
    }

Output :



No comments:

Post a Comment