Abstract Classes in C#

Abstract Classes in CSharp

This article will explain one of the most important concepts of object oriented languages (for C#), abstract classes.

Abstract Class

Abstract classes cannot be instantiated by themselves; we need to create a subclass to instantiat them. Subclasses for instantiation purposes are also called derived classes.

Abstract classes have abstract members that are derived classes must be overriden 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 noticed 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 it.

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 the derived class.

Properties

The following are the properties:

  • Abstract properties behave like abstract modifiers on static property.
  • It is an error to use an abstract access modifier on a 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();
       }
    }
}

Abstract Classes in CSharp

Now let's see what happens when we do take our implementation method out of the derived class and comment out 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:

Abstract Classes in CSharp

Now making some other modifications in the code and checking the property of the abstract class mechanism:

 

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;

namespace Hello_Word
{
    abstract class baseClas
   
{
        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();

       }

   
}

}

Abstract Classes in CSharp

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 goes through.

Up Next
    Ebook Download
    View all
    Learn
    View all