Abstract Class in C#

In this story, we will learn about abstract classes in C#.
  • 3010

Abstract Class 

We do not create a instance of the abstract class in C#. Abstract class cannot provide  implementations. Abstract class may contain non-final variables. Abstract class defines few or none of the methods.Abstract classes can have implementations for some of its members. A abstract class can have the usual flavors of class members like private, protected. 

A abstract class should be extended using keyword "extends". A abstract class also cannot be instantiated, but can be invoked if a main() exists. To use the definitions defined in the abstract class the child class inherits from the abstract class and then instances of the Child class may be easily created. Abstract class is mainly used to if you want to derive show the hierarchical information. Abstract class is also support data hiding. We have mainly used the abstract class code re-usability process. We have not create the object of the abstract class because abstract class is in incomplete. The one a most important things is if you create a abstract class so the methods of abstract class is always abstract.

Code Sample

using
 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Abtract_Class
{
    abstract class manishroom
    {
        abstract public int area();
    }
    class Squ : manishroom
    {
        int s = 0;
        public Squ(int n)
        {
            s = n;
        }
        public override int area()
        {
            return s * s;
        }
        static void Main()
        {
            Squ sq = new Squ(15);
            Console.WriteLine("Area of the manishroom= {0} ", sq.area());
            Console.ReadLine();
        }
    }
}

Output:

 123.gif

Summary 

Now in example we create a abstract class that name is "manishroom". The name of abstract methods is "area". Now through a abstract class we simply so the area. Now we see abstract class only contain a definition and derive all method which class that is inherit abstract class. In last simple create a object of a class and show the  functionality of the class.

References

Here are some good references on abstract class and related resources.

Abstract Class Resources in C# 

© 2020 DotNetHeaven. All rights reserved.