Abstract Class in C# with Example

Abstract Class in C# with real Example create abstract class derive a class from abstract class Abstract properties Abstract class inherited from Interface abstract method examples

The purpose of an abstract class is to provide a common definition of base class that multiple derived classes can share, and can be used only as a base class and never want to create the object of this class. Any class can be converted into abstract class by adding abstract modifier to it. Common features of an abstract class:

  1. An abstract class cannot be instantiated.
  2. An abstract class can contain abstract as well as non-abstract methods.
  3. An abstract method cannot have it implementation in abstract class itself.
  4. An abstract class can never have a sealed modifier, because it is opposite to the abstract class, an abstract class is created to inherit while a sealed class cannot be inherited.
  5. A class cannot be inherited from multiple abstract classes but from multiple interfaces.
  6. An abstract cannot be inherited from a class and multiple interfaces.
  7. A property can also be abstract in an abstract class.
  8. An abstract method is by default a virtual.
  9. An abstract method can only be declared in an abstract class.
  10. All derived classes must implement all the abstract methods in it.

An abstract class cab be created by using abstract modifier

public abstract class SomeClass
{
  // Class members here.
}

Let’s create an abstract class “Person” with a non-abstract and an abstract method

public abstract class Person
{
    public void CanWalk()
    {
        Console.WriteLine("Yes, I can walk");
    }
    public abstract void CanSpeakLanguages();
}

In this example CanWalk is concrete method while CanSpeakLanguages is an abstract method which must be implemented in derived classes. Now let’s derived some classes from our Abstract Person class, I used constructor to print the class name

public class American : Person
{
    public American()
    {
        Console.WriteLine("American Class:");
    }

    public override void CanSpeakLanguages()
    {
        Console.WriteLine("I can speak English");
    }
}

public class Indian : Person
{
    public Indian()
    {
        Console.WriteLine("Indian Class:");
    }
    public override void CanSpeakLanguages()
    {
        Console.WriteLine("I can speak Hindi, Urdu and English");
    }
}

public class Chinese : Person
{
    public Chinese()
    {
        Console.WriteLine("Chinese Class:");
    }
    public override void CanSpeakLanguages()
    {
        Console.WriteLine("I can speak Mandolin");
    }
}

Now we will test our classes:

Person person = new American();
person.CanWalk();
person.CanSpeakLanguages();

person = new Indian();
person.CanWalk();
person.CanSpeakLanguages();

person = new Chinese();
person.CanWalk();
person.CanSpeakLanguages();

Here is the output:

American Class:
  Yes, I can walk
  I can speak English

Indian Class: 
  Yes, I can walk
  I can speak Hindi, Urdu and English

Chinese Class: 
  Yes, I can walk
  I can speak Mandolin

As we can see here CanWalk function is printing the same content every time from every class because it is not overridden in derived classes. So it suggest us, we can use an abstract class where some methods are same for all the derived classes and some are different and need to be implemented separately in derived classes.

Abstract properties

Abstract property is similar to abstract method except the declaration 1. A static property cannot be abstract 2. Similar to abstract method, abstract properties are by default virtual. 3. An abstract property must be implemented in its derived classes.

Let’s see it in action

public abstract class Person
{
    public abstract String Name
    {
        get;
        set;
    }
    // Other code
}

And here is the derived American class

public class American : Person
{
    Int32 _age;
    public override Int32 Age
    {
        get
        {
            return _age;
        }
        set
        {
            _age = value;
        }
    }
    //Other code
}

Abstract class inherited from Interface(s)

This article cannot be completed until we will discuss abstract class inherited from an interface or from multiple interfaces. Let’s say we have to inherit an abstract class from an interface, do we need to implement all the inherited methods in a derived abstract class or we can use them as abstract methods? Answer is up to you, which method you want to implement you can and which you don’t want to implement mark them abstract e.g.

public interface IPerson
{
    String CanWalk();
    String CanSpeak();
}

public abstract class Person : IPerson
{
    public String CanWalk()
    {
        return "Yes, I can walk";
    }
    public abstract String CanSpeak();
}

As we can see we implemented CanWalk method while CanSpeak is marked as an abstract method which can be implemented in derived classes

Ali Adravi Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
  • c#
  • asp.net
  • oops
By Ali Adravi On 21 Aug, 13  Viewed: 18,742

Other blogs you may like

Polymorphism in C# with example

Polymorphism is derived from two Latin words, 1. Poly means many 2. Marphose means forms. By using inheritance, a class can be used as many types, say we have a Person which may be a Professor, may be a Student, may be Clerk. So in this article we will see some example of Polymorphism. When we... By Hamden   On 01 Jun 2013  Viewed: 4,548

How to create windows service in C# visual studio 2010

Creating windows service in C# is really easy but when we need to create first time we face many problems so in this post we will try to learn everything about window service. In this article we will create a windows service which will run after a particular interval, and time should be... By Ali Adravi   On 30 Apr 2013  Viewed: 8,610

Covariance and Contravariance in C# 4.0 with example

C# the language, right from the very beginning is always supported covariance and contravariance but in most simple scenario, now in C# 4.0 we have full support of covariance and contravariance in all circumstances, generic interface and generic delegates, other C# types already support from first... By Ali Adravi   On 29 Mar 2013  Viewed: 4,977

default value for different data type in C#

In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi   On 27 Mar 2013  Viewed: 2,599

enum in C# and conversion

An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement,... By Hamden   On 27 Mar 2013  Viewed: 4,525