Best web site - 390 Object-Oriented Programming: Polymorphism Chapter 10 3. Referring
Friday, January 18th, 2008390 Object-Oriented Programming: Polymorphism Chapter 10 3. Referring to a derived-class object with a base-class reference is safe, because the derived-class object is an object of its base class. However, this reference can refer only to base-class members. If this code refers to derived-class-only members through the base-class reference, the compiler reports an error. 4. Referring to a base-class object with a derived-class reference generates a compiler error. To avoid this error, the derived-class reference first must be cast to a base- class reference explicitly. In this cast, the derived-class reference must reference a derived-class object, or C# generates an InvalidCastException. Common Programming Error 10.3 After assigning a derived-class object to a base-class reference, attempting to reference derived- class-only members with the base-class reference is a compilation error. Common Programming Error 10.4 Treating a base-class object as a derived-class object can cause errors. Though it is convenient to treat derived-class objects as base-class objects by manipulating derived-class objects with base-class references, doing so can cause significant problems. For example, a payroll system, must be able to traverse an array of employees and calculate the weekly pay for each person. Intuition suggests that using base-class references would enable the program to call only the base-class payroll calculation routine (if there is such a routine in the base class). Using only base-class references, we can invoke the proper payroll calculation routine for each object, whether the object is a base-class object or a derived-class object. We learn how to create classes that exhibit this behavior as we introduce polymorphism throughout this chapter. 10.3 Type Fields and switch Statements One way to determine the type of an object that is incorporated in a larger program is to use a switch structure. This allows us to distinguish among object types, then invoke an appropriate action for a particular object. For example, in a hierarchy of shapes in which each shape object has a ShapeType property, a switch structure could employ the object s ShapeType to determine which Print method to call. However, using switch logic exposes programs to a variety of potential problems. For example, the programmer might forget to include a type test when one is warranted, or the programmer might forget to test all possible cases in a switch structure. When modifying a switch-based system by adding new types, the programmer might forget to insert the new cases in all relevant switch statements. Every addition or deletion of a class requires the modification of every switch statement in the system; tracking these statements down can be time consuming and error prone. Software Engineering Observation 10.3 Polymorphic programming can eliminate the need for unnecessary switch logic. By using C# s polymorphism mechanism to perform the equivalent logic, programmers can avoid the kinds of errors typically associated with switch logic.
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.