Abstract Classes in C#

This article exlains one of the most important concepts of object oriented languages, abstract classes (in reference to C#).

Abstract Classes

Abstract classes cannot be instantiated by themselves; we must need to create a subclass to initiate. A subclass for the initiation purpose is also defined as a derived classes. Abstract classes have abstract members that derived classes must override in order to provide functionality.

For example, abstract classes are defined as:

    abstract class baseClass

    {

        public abstract int myMethod(int arg1, int arg2);

    }


The point that needs to be made here is that I am not declaring the functionality here. So here I am forcing the subclass to do that. The subclass (derived class) must do this for us.

Abstract Method


An abstract method has no implementation. Its implementation logic is provided instead by classes that derive from it.We use an abstract class to create a base class for a derived class.

Properties 
  • Abstract properties behave like abstract modifier on static property.
  • It is an error to use an abstract access modifier on static property.
  • An abstract inherited property can be overridden in a derived class.
  • An abstract class must provide an implementation for the all interface members.

Example | Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Hello_Word

{

    abstract class baseClass

    {

        public abstract int myMethod(int arg1, int arg2);

    }

 

    class DerivedClass : baseClass

    {

        public override int  myMethod(int arg1, int arg2)

        {

            return arg1 + arg2;

        }

    }

 

 

    class overloding

    {

 

        public static void Main()

        {

 

            DerivedClass dc = new DerivedClass();

            int result = dc.myMethod(18, 25);

            Console.WriteLine("result: " + result);

            Console.ReadLine();

            

        }

    }

}





Now let's see what happened when we do take our implementation method out of the derived class and comment the calling part of the code. Here we go.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Hello_Word

{

    abstract class baseClass

    {

        public abstract int myMethod(int arg1, int arg2);

    }

 

    class DerivedClass baseClass

    {

       

    }

 

 

    class Program

    {

 

        public static void Main()

        {

 

            /* *

            DerivedClass dc = new DerivedClass();

            int result = dc.myMethod(18, 25);

            Console.WriteLine("result: " + result);

             * */

            Console.ReadLine();

            

        }

    }

}


This code will now show an error message as:



Now to make some other modifications in the code and check the property of the abstract class mechanism.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Hello_Word

{

    abstract class baseClass

    {

        public abstract int myMethod(int arg1, int arg2);

    }

 

    class DerivedClass : baseClass

    {

       

    }

 

 

    class Program

    {

 

        public static void Main()

        {

 

            /* *

            DerivedClass dc = new DerivedClass();

            int result = dc.myMethod(18, 25);

            Console.WriteLine("result: " + result);

             * */

            baseClass bc = new baseClass();

 

            Console.ReadLine();

            

        }

    }

}



Again we are getting some sort of errors caused by these changes.

I guess now you will be able to understand how this functionality actually works.