blog

Home / DeveloperSection / Blogs / Abstract Class in C#

Abstract Class in C#

Sumit Kesarwani3049 13-May-2013

In this article, I’m trying to explain the concept of abstract class.

An Abstract class is an incomplete class or special class we can't instantiate. We can use an Abstract class as a Base Class. An Abstract method must be implemented in the non-Abstract class using the override keyword. After overriding the abstract method is in the non-Abstract class. We can derive this class in another class and again we can override the same abstract method with it. 

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.


Features:

1.       An abstract class can inherit from a class and one or more interfaces.

2.       An abstract class can implement code with non-Abstract methods.

3.       An Abstract class can have modifiers for methods, properties etc.

4.       An Abstract class can have constants and fields.

5.       An abstract class can implement a property.

6.       An abstract class can have constructors or destructors.

7.       An abstract class cannot be inherited from by structures.

8.    An abstract class cannot support multiple inheritance.

Example

using System;
namespaceAbstractClassConsoleApplication
{
    /// <summary>
    /// declare an abstract class with keyword abstract
    /// </summary>
    abstractclassAbstract
    {
        /// <summary>
        /// declare abstract methods
        /// </summary>
        publicabstractvoidarea(int l, int b);
        publicabstractvoidcircumference(int l, int b);
    }
    classRectangle : Abstract//inherits abstract class
    {
        /// <summary>
        /// override abstract methods in derived class and give definition
        /// </summary>
        /// <param name="l">length of rectangle</param>
   /// <param name="b">breadth of rectangle</param>
        publicoverridevoidarea(int l,int b)
        {
            Console.WriteLine("Area of Square : "+(l*b));
        }
        publicoverridevoidcircumference(int l,int b)
        {
            Console.WriteLine("Circumference : " +(2*(l+b)));
        }
    }
    classProgram
    {
        staticvoidMain(string[] args)
        {
            Rectanglerec = new Rectangle(); // create object
            Console.Write("Enter Length=");
            intl = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter width=");
            intb = Convert.ToInt32(Console.ReadLine());
            rec.area(l,b); //calls the method
            rec.circumference(l,b); //call the method
        }
    }
}

Output :-

Enter Length=12

Enter width=9

Area Of Rectangle=108

Circumference=42

In this example, I have declared an abstract class with two abstract methods – area() and circumference(). Rectangle class inherits the abstract class and gives the definition to the abstract methods. In program class create object of rectangle class and call both the methods.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By