Wednesday, May 8, 2013

Abstract Class in C#



Introduction

An abstract class. oooo! sorry, To knowing abstract class First you should known about what is "abstract", So it is a keyword, that enables you to create a class or classes members that are incomplete and must be implemented in derived class. The purpose to describing abstract keyword is that it must write with the class name, because without this keyword you cannot create abstract class, Now lets move with abstract class:

"An abstract class means that, no object of this class can be instantiated, but can make derivation of this"  or simply say "An abstract keywords that enables you to create class or classes that are incomplete and must be implemented in derived class and it is one of the essential provide by .Net".


Declaration of abstract class

public abstract class Demo
        {
            //class member here
        }
Define abstract method

public abstract class Demo
        {
            public abstract void example();
        }
Simple abstract class Example

using System;
public
abstract class Demo
{
    public void nonAbstractMeth()  // non abstract method
    {
        Console.WriteLine("I am a non abstract method");
    }
    public abstract void example(); //abstract method
}
class Implementation:Demo
{
    public override void example()
    {
        Console.WriteLine("Abstract method implementation!");
    }
    public static void Main()
    {
        Implementation o = new Implementation();
        o.example();
        o.nonAbstractMeth();
    }
}
Output


Abstract method implementation
I am a non abstract method

Now Lets see what we can not do with abstract class

Point 1

An abstract class cannot be sealed or static

For example

using System;
public
abstract sealed class Demo
{
   
public abstract void example(); //abstract method
}

class Implementation:Demo
{
    public override void example()
    {
        Console.WriteLine("Abstract method implementation!");
    }
    public static void Main()
    {
        Implementation o = new Implementation();
        o.example();
    }
}
When you compile the above code then it will gives an error " 'Demo': an abstract class cannot be sealed or static", Can you think, why csc (c-sharp complier gives that error), because the "sealed keyword say that do not use me with that class, who you want to inherit."

Point 2

An abstract method cannot be private.

For example
using System;
public abstract class Demo
{
    private abstract void example(); //abstract method
}
 
When you compile the above code then it will gives an error " 'Demo.example()': virtual or abstract members cannot be private". Can you think, why csc (c-sharp complier gives that error), because when you declared an abstract method with private, so this method should be same in both the abstract and its derived class, and when you try to implement that method in sub class, then compiler raises an error because you can not access private methods in its sub class.

Point 3


You can not write different- different access modifier of the abstract method in the both abstract class and its derived class.
using System;
public abstract  class Demo
{
    public abstract void example(); //abstract method
}
class Implementation:Demo
{
    protected override void example()
    {
        Console.WriteLine("Abstract method implementation!");
    }
    public static void Main()
    {
        Implementation o = new Implementation();
        o.example();
    }
}

When you compile the above code then it will gives an error "
'Implementation.example()': cannot change access modifiers when overriding 'public' inherited member 'Demo.example()' ".
Point 4

An abstract method cannot have the modifier virtual because its method is implicitly virtual.
using System;
public abstract  class Demo
{
    public virtual abstract void example(); //abstract method
}
When you compile the above code the compiler will raises an error " The abstract method 'Demo.example()' cannot be marked virtual ".

Like as interface abstract modifier can be used with classes, methods, properties, indexers and events.
 
Example of abstract class (with properties)
using System;
public abstract class Demo
{
    public abstract void example(); //abstract method
    public abstract int Prop1
    {
        get;
        set;
    }
    public abstract string Prop2
    {
        get;
        set;
    }
}
class Implementation : Demo
{
    private int prop1=1;
    private string prop2="DotnetHearts";
    public override void example()
    {
        Console.WriteLine("Abstract method implementation!");
    }
    public override int Prop1
{
         get
       {
        return this.prop1;
       }
         set
    {
        this.prop1 = value;
             
       }
}
   
    public override string Prop2
    {
        get
        {
            return this.prop2; 
        }
        set
        {
            this.prop2 = value;
        }
    }
}
class Mainclass:Implementation
{
    public static void Main()
    {
        Implementation obj = new Implementation();
        obj.example();
        Console.WriteLine("Rank of " + obj.Prop2 + " is " + obj.Prop1);
        Console.ReadKey();
    }
}

Output
Abstract method implementation
Rank of DotnhetHearts is 1