Fri , Nov 24 2023
An abstract class is an incomplete class or special class, we can't be instantiated (We cant create object of this kind of class).
Its intendent to be a base(Parent) class.
The purpose of an abstract class is to provide a blueprint for derived classes and set some rules what the derived classes must implement when they inherit an abstract class. Abstract classes are used to define a common set of behaviors or properties that derived classes should have.
To create an abstract class in C#, you use the “abstract” keyword before the class definition. Here is an example:
using System;
public abstract class Vehicle {
public abstract string Brand { get; }
public abstract string Model { get; }
public abstract int Year { get; }
public abstract void Start ();
public abstract void Stop ();
}
public class Car : Vehicle {
public override string Brand => "Ford";
public override string Model => "Mustang";
public override int Year => 2022;
public override void Start () {
Console.WriteLine ("Starting the car...");
}
public override void Stop () {
Console.WriteLine ("Stopping the car...");
}
}
public class Motorcycle : Vehicle {
public override string Brand => "Harley-Davidson";
public override string Model => "Iron 883";
public override int Year => 2021;
public override void Start () {
Console.WriteLine ("Starting the motorcycle...");
}
public override void Stop () {
Console.WriteLine ("Stopping the motorcycle...");
}
}
class Program {
static void Main (string[] args) {
Vehicle [] vehicles = new Vehicle [] { new Car (), new Motorcycle () };
foreach (Vehicle vehicle in vehicles) {
Console.WriteLine ($"The {vehicle.Brand} {vehicle.Model} ({vehicle.Year})");
vehicle.Start ();
vehicle.Stop ();
}
}
}
In this example, we have an abstract class named Vehicle
that has four abstract properties: Brand
, Model
, Year
, Start
, and Stop
. The Car
and Motorcycle
classes inherit from the Vehicle
class and provide their own implementation of these properties.
The Car
class has a Brand
, Model
, and Year
properties and implements the Start
and Stop
methods to start and stop the car.
The Motorcycle
class has Brand
, Model
, and Year
properties and implements the Start
and Stop
methods to start and stop the motorcycle.
In the Main
method, we create an array of Vehicle
objects that contains a Car
and a Motorcycle
. We then loop through the array and output the brand, model, and year of each vehicle, and start and stop each vehicle.
#tags:
#abstract class in c#,
#interface and abstract class in c#,
#why we use abstract class in c#,
#use of abstract class in c#,
#when to use interface and abstract class in c#,
#how to create instance of abstract class in c#,
#how to implement abstract class in c#,
#how to call method from abstract class in c#,
#inheritance and abstract class in c#,
#how to mock abstract class in c#,
#abstract method and abstract class in c#,
#abstract class in c# and interface,
#abstract classes in c# with an example,
#abstract class advantages c#,
#abstract class attributes c#,
#abstract class animal c#,
#abstract class algorithm c#,
#abstract class example in c# console application,
#abstract class memory allocation c#,
#abstract class and interface in c# with real time example,
!! Do Share !!
Hope you found this article helpful.
Thanks for your time, keep reading happy reading at lifeDB.in, please do follow, like comment.
Instagram Page :Follow lifedb_official on insta
Facebook Profile:Follow Lifedb_official page on facebook
Linkedin Profile:Follow lifedb_official on linkdin
Twitter Profile: Follow lifedb_official on X
I am an engineer with over 10 years of experience, passionate about using my knowledge and skills to make a difference in the world. By writing on LifeDB, I aim to share my thoughts, ideas, and insights to inspire positive change and contribute to society. I believe that even small ideas can create big impacts, and I am committed to giving back to the community in every way I can.
Leave a Reply