Virtual keyword can be used to start a new inheritance ladder : 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Class Interface » Override VirtualScreenshots 
Virtual keyword can be used to start a new inheritance ladder
 

using System;

public class Class1 {
    public static void Main(string[] strings) {
        BankAccount ba = new BankAccount();
        Test1(ba);

        SavingsAccount sa = new SavingsAccount();
        Test1(sa);
        Test2(sa);

        SpecialSaleAccount ssa = new SpecialSaleAccount();
        Test1(ssa);
        Test2(ssa);
        Test3(ssa);

        SaleSpecialCustomer ssc = new SaleSpecialCustomer();
        Test1(ssc);
        Test2(ssc);
        Test3(ssc);
        Test4(ssc);
    }

    public static void Test1(BankAccount account) {
        Console.Write("to Test(BankAccount)");
        account.Withdrawal(100);
    }

    public static void Test2(SavingsAccount account) {
        Console.Write("to Test(SavingsAccount)");
        account.Withdrawal(100);
    }

    public static void Test3(SpecialSaleAccount account) {
        Console.Write("to Test(SpecialSaleAccount)");
        account.Withdrawal(100);
    }

    public static void Test4(SaleSpecialCustomer account) {
        Console.Write("to Test(SaleSpecialCustomer)");
        account.Withdrawal(100);
    }
}

public class BankAccount {
    virtual public void Withdrawal(double dWithdrawal) {
        Console.WriteLine(" invokes BankAccount.Withdrawal()");
    }
}

public class SavingsAccount : BankAccount {
    override public void Withdrawal(double dWithdrawal) {
        Console.WriteLine(" invokes SavingsAccount.Withdrawal()");
    }
}
public class SpecialSaleAccount : SavingsAccount {
    new virtual public void Withdrawal(double dWithdrawal) {
        Console.WriteLine(" invokes SpecialSaleAccount.Withdrawal()");
    }
}

public class SaleSpecialCustomer : SpecialSaleAccount {
    override public void Withdrawal(double dWithdrawal) {
        Console.WriteLine
          (" invokes SaleSpecialCustomer.Withdrawal()");
    }
}

 
Related examples in the same category
1.Polymorphism
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.