abstract class attributes c#, design abstract methods and property, Free C# Tutorial for Beginners

C# Abstract Class Example

In this tutorial you learn about abstract class in C# with examples, how to define an abstract class in C#, abstract method implementation etc, abstract and non-abstract methods and properties.

When to use Abstract class?

Abstract class is a class with core design definition without implementation; it helps to standardize object features and design across application, for example if we are designing a school management system, there we can create an abstract class “Person”, and make sure all type of person (Student, Teacher, Administrator, Peon etc) in the system inherit from this abstract class.

So in “Person” class whatever methods and properties we define, everyone (every type of person) will have the same implementation.

C# Abstract class Advantages

An abstract class is known as a base class; contain the most basic definition of a particular entity, an abstract class can contain abstract and non-abstract properties, methods

Abstract Class Characteristic

Here are some core characteristic of an abstract class in C#.

  • An abstract class cannot support multiple inheritance.
  • An abstract class can implement code with non-Abstract methods.
  • An Abstract class can have constants and fields.
  • An abstract class can implement a property.
  • An abstract class can have constructors or destructors.
  • An abstract class cannot be inherited from by structures.
  • An Abstract class can have modifiers for methods, properties etc.
  • An abstract calss can inherit from a class and one or more interfaces.
How to write a Abstract Class?

An abstract class start with an abstract keyword (known as abstract modifier ), can have abstract and non-abstract method and propertise

Abstract class declaration c#

Here in example below you can see we have abstract methods, properties and also notice we can have implementation in abstract class itself, “StopTraining” method is an example of body implementation with in abstract class.

abstract class TrainingBase
{
    public abstract string Title { get; set; }
    public abstract DateTime TrainingDate { get; set; }
    protected abstract string Subjects { get; set; }
    public abstract string Trainer { get; set; }
    public abstract string Location { get; set; }
    public abstract void StartTraining();                
    protected void StopTraining()
    {
        if (TrainingDate != DateTime.Now)
        {
            Console.WriteLine("Training is not Active");
        }
    }
}

As you can see in above TrainingBase abstract class has abstract and non abstract methods and abstract properties

Remember you cannot create a new instance of an abstract class directly, for example, following piece of code will not compile.

    TrainingBase obj=new TrainingBase();

c# abstract class implementation

Now you learn how to implement the abstract class in child class, see in example below how c# abstract class force method implementation in child class

class WEBApiTraining : TrainingBase
{
    string trainer = "WebTrainingRoom";
    string title = "Web API Training Room";
    string subjects = "put, post, get, exception";
    string location = "Online";
    DateTime tDate = DateTime.Now;
    public override void StartTraining()
    {
        Console.WriteLine("Start Web API Training");
    }
    public override string Title
    {
        get { return title; }
        set { title = value; }
    }
    public override DateTime TrainingDate
    {
        get { return tDate; }
        set { tDate = value; }
    }
    protected override string Subjects
    {
        get { return subjects; }
        set { subjects = value; }
    }
public override string Trainer
{
    get { return trainer; }
    set { trainer = value; }
}

Now we can create multiple training classes that will be inherited from same TrainingBase abstract class

c# abstract class force method implementation

Look at the picture below, we have implemented the above base class into two different child class, and how abstract class force implementation of abstract methods and properties in both child classes.

abstract class example

Notice, in above example I have written an abstract class called TrainingBase, then I have two child class called “WebApiTraining” and “JQueryTraining”, now both trainings are inherited from “TrainingBase” class

Hope now you have good understanding about abstract class!

You should also read

 
free C# tutorial online


Interface and Abstract Class in C#
Other Online Tutorial

C# .net Interview Questions Answers

.Net C# Examples | Join .Net C# Course