Polymorphism
using System;
public class MotorVehicle {
public string make;
public string model;
public MotorVehicle(string make, string model) {
this.make = make;
this.model = model;
}
public virtual void Accelerate() {
Console.WriteLine(model + " accelerating");
}
}
public class Product : MotorVehicle {
public Product(string make, string model) :
base(make, model) {
}
public override void Accelerate() {
Console.WriteLine("Pushing gas pedal of " + model);
base.Accelerate();
}
}
public class Motorcycle : MotorVehicle {
public Motorcycle(string make, string model) :
base(make, model) {
// do nothing
}
public override void Accelerate() {
Console.WriteLine("Twisting throttle of " + model);
base.Accelerate();
}
}
class MainClass {
public static void Main() {
Product myProduct = new Product("Toyota", "MR2");
myProduct.Accelerate();
Motorcycle myMotorcycle =
new Motorcycle("Harley-Davidson", "V-Rod");
myMotorcycle.Accelerate();
}
}
Related examples in the same category