Home >Backend Development >C#.Net Tutorial >Abstract keywords in C#
#The abstract keyword in C# is used for abstract classes. Abstract classes in C# include abstract methods and non-abstract methods. You cannot instantiate an abstract class.
Example of abstract class Vehicle and abstract method display() -
public abstract class Vehicle { public abstract void display(); }
Abstract class has derived classes: Bus, Car and Motorcycle. The following is the implementation of the Car derived class-
public class Car : Vehicle { public override void display() { Console.WriteLine("Car"); } }
The following is the example of the abstract class in C#-
Live demonstration
using System; public abstract class Vehicle { public abstract void display(); } public class Bus : Vehicle { public override void display() { Console.WriteLine("Bus"); } } public class Car : Vehicle { public override void display() { Console.WriteLine("Car"); } } public class Motorcycle : Vehicle { public override void display() { Console.WriteLine("Motorcycle"); } } public class MyClass { public static void Main() { Vehicle v; v = new Bus(); v.display(); v = new Car(); v.display(); v = new Motorcycle(); v.display(); } }
Bus Car Motorcycle
The above is the detailed content of Abstract keywords in C#. For more information, please follow other related articles on the PHP Chinese website!