Free mag vol1 | Page 293

CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM } public override void GiveBonus(float amount) { base.GiveBonus(amount); Random r = new Random(); StockOptions += r.Next(500); } Notice how each overridden method is free to leverage the default behavior using the base keyword. In this way, you have no need to completely reimplement the logic behind GiveBonus(), but can reuse (and possibly extend) the default behavior of the parent class. Also assume that the current DisplayStats() method of the Employee class has been declared virtually. By doing so, each subclass can override this method to account for displaying the number of sales (for salespeople) and current stock options (for managers). For example, consider the Manager’s version of the DisplayStats() method (the SalesPerson class would implement DisplayStats() in a similar manner to show the number of sales): public override void DisplayStats() { base.DisplayStats(); Console.WriteLine("Number of Stock Options: {0}", StockOptions); } Now that each subclass can interpret what these virtual methods means to itself, each object instance behaves as a more independent entity: static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // A better bonus system! Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.ReadLine(); } The following output shows a possible test run of your application thus far: 230