Web hosting service - Chapter 10 Object-Oriented Programming: Polymorphism 389 Line 28

Chapter 10 Object-Oriented Programming: Polymorphism 389 Line 28 casts point2, which currently refers to a Circle object (circle1), to a Circleand assigns the result to circle2. As we discuss momentarily, this cast would be dangerous if point2 were referencing a Point. Lines 30 31 invoke method ToStringof the Circle object to which circle2 now refers (note that the fourth line of the output demonstrates that Circle s ToString method is called). Lines 33 34 calculate and output circle2 s Area. Line 39 explicitly casts reference point1 to a Circle. This is a dangerous operation, because point refers to a Point object, and a Point is not a Circle. Objects can be cast only to their own type or to their base-class types. If this statement were to execute, C# would determine that point1 references a Point object, recognize the cast to Circle as dangerous and indicate an improper cast with an InvalidCastException message. However, we prevent this statement from executing by including an if/else structure (lines 37 45). The condition at line 37 uses keyword is to determine whether the object to which point1refers is a Circle. Keyword is discovers the type of the object to which the left operand refers and compares this type to the right operand (in this case, Circle). In our example, point1 does not refer to a Circle, so the condition fails, and line 44 appends to output a string that indicates the result. Note that the is comparison will be true if the left operand is a reference to an instance of the right operand or a derived class. Common Programming Error 10.2 Attempting to cast a base-class reference to a derived-class type causes an Invalid- CastException if the reference refers to a base-class object rather than an appropriate derived-class object. Software Engineering Observation 10.2 The is keyword enables a program to determine whether a cast operation would be successful by ensuring that the reference type and target type are compatible. If we remove the if test and execute the program, C# displays a MessageBox that contains the message: An unhandled exception of type ‘System.InvalidCastException’ occurred in followed by the name and path of the executing program. We discuss how to deal with such situations in Chapter 11. Despite the fact that a derived-class object also is a base-class object, the derived- class and base-class objects are different. As we have discussed previously, derived-class objects can be treated as if they were base-class objects. This is a logical relationship, because the derived class contains members that correspond to all members in the base class, but the derived class can have additional members. For this reason, assigning a base- class object to a derived-class reference is not allowed without an explicit cast. Such an assignment would leave the additional derived-class members undefined. There are four ways to mix base-class references and derived-class references with base-class objects and derived-class objects: 1. Referring to a base-class object with a base-class reference is straightforward. 2. Referring to a derived-class object with a derived-class reference is straightforward.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Leave a Reply