Abstract keywords in C#

王林
Release: 2023-08-31 16:01:06
forward
556 people have browsed it

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

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

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

Output

Bus
Car
Motorcycle
Copy after login

The above is the detailed content of Abstract keywords in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template