static void Main(string[] args) { RDoctor r = new RDoctor("Mr.Samaram", "6969696969", 30000); Consultant c = new Consultant("Sherman", "98764504", 5, 2000); Print(r); Print(c); Doctor r2 = new RDoctor("Samram 2", "55445333", 116); // The above line was possible because the object reference of base class can point to an // derived class object.(The universal truth is Derived class IS A Base class) r2.Print(); // Now runs correctly }
public static void Print(Consultant x) { x.Print(); int sal = x.GetSalary(); if (sal <= 20000) { Console.WriteLine("Pay by check"); } else { Console.WriteLine("Pay by cash"); } }
static void Main(string[] args) { RDoctor r = new RDoctor("Mr.Samaram", "6969696969", 30000); Consultant c = new Consultant("Sherman", "98764504", 5, 2000); Print(r); Print(c); Doctor r2 = new RDoctor("Samram 2", "55445333", 116); // The above line was possible because the object reference of base class can point to an // derived class object.(The universal truth is Derived class IS A Base class) r2.Print(); // The above r2.Print() calls the print from class doctor but not RDoctor. To overcome this // problem and combine below two overloaded methods we use Runtime Polymorphism }