Chapter 10 (Web design tools) Object-Oriented Programming: Polymorphism 391 Testing and
Chapter 10 Object-Oriented Programming: Polymorphism 391 Testing and Debugging Tip 10.1 An interesting consequence of using polymorphism is that programs take on a simplified appearance. They contain less branching logic and more simple, sequential code. This simplification facilitates testing, debugging and program maintenance. 10.4 Polymorphism Examples In this section, we discuss several examples of polymorphism. If class Rectangle is derived from class Quadrilateral, then a Rectangle object is a more specific version of a Quadrilateralobject. Any operation (such as calculating the perimeter or the area) that can be performed on an object of class Quadrilateral also can be performed on an object of class Rectangle. Such operations also can be performed on other kinds of Quadrilaterals, such as Squares, Parallelograms and Trapezoids. When a program invokes a derived-class method through a base-class (i.e., Quadrilateral) reference, C# polymorphically chooses the correct overriding method in the derived class from which the object was instantiated. We investigate this behavior in later examples. Suppose that we design a video game that manipulates objects of many different types, including objects of classes Martian, Venutian, Plutonian, SpaceShip and LaserBeam. Also, imagine that each of these classes inherits from the common base class called SpaceObject, which contains method DrawYourself. Each derived class implements this method. A screen-manager program would maintain a container (such as a SpaceObject array) of references to objects of the various classes. To refresh the screen, the screen manager would periodically send each object the same message namely, DrawYourself. However, each object responds in a unique way. For example, a Martian object would draw itself in red with the appropriate number of antennae. A SpaceShip object would draw itself as a bright, silver flying saucer. A LaserBeam object would draw itself as a bright red beam across the screen. Thus the same message sent to a variety of objects would have many forms of results hence the term polymorphism. A polymorphic screen manager facilitates adding new classes to a system with minimal modifications to the system s code. Suppose we want to add class Mercurians to our video game. To do so, we must build a class Mercurian that inherits from SpaceObject, but provides its own definition of method DrawYourself. Then, when objects of class Mercurian appear in the container, the programmer does not need to modify the code for the screen manager. The screen manager invokes method Draw- Yourself on every object in the container, regardless of the object s type, so the new Mercurian objects simply plug right in. Thus, without modifying the system (other than to build and include the classes themselves), programmers can use polymorphism to include additional types of classes that were not envisioned when the system was created. With polymorphism, one method can cause different actions to occur, depending on the type of the object on which the method is invoked. This gives the programmer tremendous expressive capability. In the next several sections, we provide examples that demonstrate polymorphism. Software Engineering Observation 10.4 With polymorphism, the programmer can deal in generalities and let the execution-time environment concern itself with the specifics. The programmer can command a wide variety of objects to behave in manners appropriate to those objects, even if the programmer does not know the objects types.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.