Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

Abstract Class in C#, C# tutorial

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 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

Now you learn how to consume this abstract 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
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#
Learn C# Online Tutorial


Group Training