Polymorphism : Override Virtual « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Override VirtualScreenshots 
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
1.Virtual keyword can be used to start a new inheritance ladder
2.Virtual and overloadVirtual and overload
3.Class hierarchy: override and virtual
4.Use virtual methods and polymorphismUse virtual methods and polymorphism
5.Demonstrate a virtual methodDemonstrate a virtual method
6.When a virtual method is not overridden, the base class method is usedWhen a virtual method is not overridden,     the base class method is used
7.illustrates polymorphismillustrates polymorphism
8.Demonstrates the use of a virtual method to override a base class methodDemonstrates the use of a virtual method to override a base class method
9.Demonstrates the use of a virtual property to override a base class propertyDemonstrates the use of a virtual property to override a base class property
10.Method override 3Method override 3
11.Test Polymorphism Virtual Functions
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.