Introduction

The Abstract keyword is used to make Abstract Class in C#. Abstract classes are not complete so we can not create an instance of this classes. An Abstract class can only be a base class of some another class.
This type of class can have both abstract and non-abstract methods inside it. Abstract members do not have any implementation body part in the abstract class, but it has to be provided in its derived class.

Example of Abstract Class in C#

using System;

namespace AbstractDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            MakeCalculation obj = new MakeCalculation();

            Console.WriteLine("Sum of 3 + 8 = " + obj.AddNumber(3, 8)); // Calling and displaying result of abstract class methods.

            Console.WriteLine("Multiplication of 3 * 8 = " + obj.MultiplyNumber(3, 8));

            Console.Read();
        }
    }

    abstract class Calculation
    {
        public abstract int AddNumber(int a, int b); // Method with only declaration i.e. Abstract Methods

        public int MultiplyNumber(int a, int b) // Method with implementation details 
        {
            return a * b;
        }
    }

    class MakeCalculation : Calculation // Inheriting abstract class.
    {
        public override int AddNumber(int a, int b) // Providing implementation details for abstract methods.
        {
            return a + b;
        }       
    }
}
Abstract Class in C#

AbstractResult

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website