Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    An illustration of type casting (C)v.Lecture 7 - slide 34 : 40
Program 3
using System; 

class App {

  public static void Main(){
  
    BankAccount ba1 = new BankAccount("George", 1000.0M, 0.01),
                ba2 = new CheckAccount("Bill", 2000.0M, 0.01),
                baRes1, baRes2, baRes3, baRes4, baRes5, baRes6; 

    CheckAccount ca = new CheckAccount("John", 2000.0M, 0.01);

    baRes1 = (BankAccount)ba1;     // OK.  But b1 is already of static 
                                   //      type BankAccount
    baRes2 = (CheckAccount)ba1;    // Illegal downcasting. Run-time error

    baRes3 = (BankAccount)ba2;     // OK.  But ba2 is already of static
                                   //      type BankAccount
    baRes4 = (CheckAccount)ba2;    // OK because ba2 refers to a CheckAccount

    baRes5 = (BankAccount)ca;      // OK.  Legal upcasting.
    baRes6 = (CheckAccount)ca;     // OK.  But ca is already of static 
                                   //      type CheckAccount
  }

}