Abstract class in C# - ProgramIdea

Abstract Class in C#


The class which cannot be instantiated known as abstract class. Abstract class has no object. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. Abstract class can contain abstract and non-abstract members. Abstract member has only declaration and non-abstract member has declaration and implementation in abstract class. Abstract class only allow other classes to inherit from it and abstract members must be implemented by classes that derive from the abstract class.

The advantage of abstract class is that it enforces certain hierarchies for all the subclasses. Class declare as abstract class by putting the abstract keyword before the class keyword in the class definition.

Properties of abstract class:

1. Abstract class can contain only abstract members or only non-abstract members or both.

2. Abstract class cannot be instantiated.

3. Abstract method cannot be static.

4. Abstract method cannot be virtual.

5. Abstract method cannot be private.

6. A class cannot be inherit more than one abstract class.

7. Abstract class can contain properties.

8. Abstract method can be overloaded.

Below a simple example of abstract class Test which have one abstract method Multiply and one non-abstract method Square. This class is inherited by Program class and it implemented abstract method using override keyword, override keyword indicate that this method declared somewhere. For accessing to methods of abstract class we create a object of Program class instead of abstract class because abstract class does not allow to create their object anywhere.

namespace Abstraction
{
    // Abstract class declaration
    abstract class Test
    {
        // Non abstract method
        public int Square(int a)
        {
            return a * a;
        }
        // Abstract method
        public abstract int Multiply(int a, int b);
    }
 

    class Program : Test
    {
        static void Main(string [] args)
        {
            Program pro = new Program();          

            // Access non-abstract method
            int square = pro.Square(2); 

            // Access abstract method
            int multiply = pro.Multiply(5, 4); 

            Console.WriteLine("Square - "  + square);
            Console.WriteLine("Multiply - "  + multiply);
         } 

        // Implement abstract method in derived class
        public override int Multiply(int x, int y)
        {
            return x * y;
        }       
    } 
}

// Output
// Square - 4
// Multiply - 20

1. Abstract class can contain only abstract method or only non-abstract method or both.

// Abstract class can contain only non-abstract method only
    abstract class Test
    {
        // Non abstract method
        public int Square(int a)
        {
            return a * a;
        }
    }

    // Abstract class can contain only abstract method only
    abstract class Test
    {      
        // Abstract method
        public abstract int Multiply(int a,int b);
    }

    // Abstract class can contain both abstract and non-abstract method 
    abstract class Test
    {
        // Non abstract method
        public int Square(int a)
        {
            return a * a;
        } 

        // Abstract method
        public abstract int Multiply(int a,int b);
    }

2. Abstract class has no object means an abstract class cannot be instantiated.

class Program : Test
{
    static void Main(string [] args)
    { 
        // You cannot create object of Abstract class, like this:
        Test test = new Test();
        // It will throw an exception like:
        // Cannot create an instance of the abstract class or interface 'Abstraction.Test'
    }
}

3. Abstract method cannot be declare as static.

public abstract static int Multiply(int a,int b);
// It will throw an exception, like:
// A static member 'Test.Multiply(int, int)' cannot be marked as override, virtual, or abstract

4. Abstract method can not be declare as virtual.

public abstract virtual int Multiply(int a,int b);
// It will throw an exception, like:
// The abstract method 'Test.Multiply(int, int)' cannot be marked virtual

5. Abstract class can not be declare as private.

private abstract int Multiply(int a,int b);
// It will throw an exception, like:
// 'Test.Multiply(int, int)': virtual or abstract members cannot be private

6. A class cannot inherit more than one abstract class.

abstract class Test
{ 

}

abstract class Test1
{ 

}

// It will throw an exception
//Error 'Program' cannot have multiple base classes: 'Test' and 'Test1'
class Program : Test , Test1
{

}

7. An abstract class may contain property or accessor

abstract class Test
{
    // Define property
    public string message { get; set ; } 

    // Abstract method
    public abstract void DisplayMessage();
} 

class Program : Test
{           
    static void Main(string [] args)
    {  
        Program pro = new Program();
        // Access property
        pro.message = "Hello ProgramIdea";

        // Access abstract method
        pro.DisplayMessage();
        Console.Read();
    } 
    // Implement abstract method in derived class
    public override void DisplayMessage()
    {           
        // Use property from abstract class
        Console.WriteLine(message);
    }      
}

// Output
// Hello ProgramIdea

8. Abstract method can be overloaded. You can define number of overloaded method in abstract class, but make sure that all overloaded method must be implemented in drive class, otherwise it will throw exception.

abstract class Test
{
    // Abstract method
    public abstract int Multiply(int a, int b); 

    // Abstract method overload
    public abstract int Multiply(int a, int b, int c);
} 

class Program : Test
{           

    static void Main(string [] args)
    {                   

        Program pro = new Program();            

        // Access abstract method
        int i = pro.Multiply(2, 3);

        // Access abstract method
        int j = pro.Multiply(4, 5, 6); 

        Console.WriteLine("First method Multiply - "  + i);
        Console.WriteLine("Second method Multiply - "  + j);
        Console.Read();
    } 

    // Implement abstract method
    public override int Multiply(int a, int b)
    {
        return a * b;
    }

    // Implement overload abstract method
    public override int Multiply(int a, int b, int c)
    {
        return a * b * c;
    }       
}

// Output
// First method Multiply - 6
// Second method Multiply - 120

9. If a class inherit an abstract class then need to implement all abstract method otherwise it will throw exception

// Exception     
//'Abstraction.Program' does not implement inherited abstract member 'Abstraction.Test.Multiply(int, int)'

abstract class Test
{
    // Abstract method
    public abstract int Multiply(int a, int b);
} 

class Program : Test
{
    static void Main(string [] args)
    {         

    }
}