Free Web Development Online Tutorials, Learn to Code
abstract class in c# and use, design abstract methods and property, Free C# Tutorial for Beginners

Abstract Class in C# Asp.net

In this tutorial we learn about Abstract Class in C#, How to define an abstract class in C#, and implementation.

What is Abstract Class?

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

Here are some quick overview of an abstract class

  • 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, 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 properties

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

Hope now have good idea about abstract class and Interface in C#, You should also check the difference between Abstract class and Interface

Comment
Name
Email
Website
Subscribe
 

Interface and Abstract Class in C#
Other Online Tutorial

C# .net Interview Questions Answers