Polymorphism : Override Virtual : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » Override Virtual »

 

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

    }

}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» Override Virtual