Abstract Classes and Abstract Methods in C#.NET


The purpose of abstract class is to provide default functionality to its sub classes.

When a method is declared as abstract in the base class then every derived class of that class must provide its own definition for that method.

 An abstract class can also contain methods with complete implementation, besides abstract methods.

When a class contains at least one abstract method, then the class must be declared as abstract class.

It is mandatory to override abstract method in the derived class.

When a class is declared as abstract class, then it is not possible to create an instance for that class. But it can be used as a parameter in a method.

Abstract class in c# example



The following example creates three classes shape, circle and rectangle where circle and rectangle are inherited from the class shape and overrides the methods Area() and Circumference() that are declared as abstract in Shape class and as Shape class contains abstract methods it is declared as abstract class.


using System;

namespace ProgramCall
{

    //Abstract class
    abstract class Shape1
    {

        protected float R, L, B;

        //Abstract methods can have only declarations
        public abstract float Area();
        public abstract float Circumference();

    }


    class Rectangle1 : Shape1
    {
        public void GetLB()
        {
            Console.Write("Enter  Length  :  ");

            L = float.Parse(Console.ReadLine()); 

            Console.Write("Enter Breadth : ");

            B = float.Parse(Console.ReadLine());
        }

        
        public override float Area()
        {
            return L * B;
        }

        public override float Circumference()
        {
            return 2 * (L + B);
        }

    }


    class Circle1 : Shape1
    {

        public void GetRadius()
        {

            Console.Write("Enter  Radius  :  ");
            R = float.Parse(Console.ReadLine());
        }

        public override float Area()
        {
            return 3.14F * R * R;
        }
        public override float Circumference()
        {
            return 2 * 3.14F * R;

        }
    }
    class MainClass
    {
        public static void Calculate(Shape1 S)
        {

            Console.WriteLine("Area : {0}", S.Area());
            Console.WriteLine("Circumference : {0}", S.Circumference());

        }
        static void Main()
        {

            Rectangle1 R = new Rectangle1();
            R.GetLB();
            Calculate(R);

            Console.WriteLine();

            Circle1 C = new Circle1();
            C.GetRadius();
            Calculate(C);

            Console.Read();

        }
    }
}

Output

Enter  Length  :  10
Enter Breadth : 12
Area : 120
Circumference : 44

Enter  Radius  :  5
Area : 78.5
Circumference : 31.4

Note

In the above example method calculate takes a parameter of type Shape1 from which rectangle1 and circle1 classes are inherited.
 
A base class type parameter can take derived class object as an argument. Hence the calculate method can take either rectangle1 or circle1 object as argument and the actual argument in the parameter S will be determined only at runtime and hence this example is an example for runtime polymorphism.




abstract class c#, abstract class c# Example, abstract method in c#, c# abstract property, c# abstract method, c# abstract class, abstract classes c#.net