Home >Backend Development >C#.Net Tutorial >Abstract keywords in C#

Abstract keywords in C#

王林
王林forward
2023-08-31 16:01:06586browse

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");
   }
}

Example

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();
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete