392 Object-Oriented Programming: Polymorphism Chapter 10 (Web design templates) Software Engineering
392 Object-Oriented Programming: Polymorphism Chapter 10 Software Engineering Observation 10.5 Polymorphism promotes extensibility. Software used to invoke polymorphic behavior is written to be independent of the types of the objects to which messages (i.e., method calls) are sent. Thus, programmers can include into a system additional types of objects that respond to existing messages and can do this without modifying the base system. 10.5 Abstract Classes and Methods When we think of a class as a type, we assume that programs will create objects of that type. However, there are cases in which it is useful to define classes for which the programmer never intends to instantiate any objects. Such classes are called abstract classes. Because such classes normally are used as base classes in inheritance hierarchies, we refer to such classes as abstract base classes. These classes cannot be used to instantiate objects, since abstract classes are incomplete. Derived classes must define the missing pieces. Abstract classes normally contain one or more abstract methods or abstract properties, which are methods and properties that do not provide implementations. Derived classes must override inherited abstract methods and properties to enable objects of those derived classes to be instantiated. We discuss abstract classes extensively in Section 10.6 and Section 10.8. The purpose of an abstract class is to provide an appropriate base class from which other classes may inherit. Classes from which objects can be instantiated are called concrete classes. Such classes provide implementations of every method and property they define. We could have an abstract base class TwoDimensionalObject and derive such concrete classes as Square, Circle and Triangle. We could also have an abstract base class ThreeDimensionalObject and derive such concrete classes as Cube, Sphere and Cylinder. Abstract base classes are too generic to define real objects; we need to be more specific before we can think of instantiating objects. For example, if someone tells you to draw the shape, what shape would you draw? Concrete classes provide the specifics that make it reasonable to instantiate objects. A class is made abstract by declaring it with keyword abstract. An inheritance hierarchy does not need to contain any abstract classes, but, as we will see, many good object- oriented systems have class hierarchies headed by abstract base classes. In some cases, abstract classes constitute the top few levels of the hierarchy. A good example of this is the shape hierarchy in Fig. 9.3. The hierarchy begins with abstract base-class Shape. On the next level of the hierarchy, we have two more abstract base classes, namely TwoDimensionalShape and ThreeDimensionalShape. The next level of the hierarchy would define concrete classes for two-dimensional shapes, such as Circle and Square, and for three-dimensional shapes, such as Sphere and Cube. Software Engineering Observation 10.6 An abstract class defines a common set of public methods and properties for the various members of a class hierarchy. An abstract class typically contains one or more abstract methods and properties that derived classes will override. All classes in the hierarchy can use this common set of public methods and properties. Abstract classes must specify signatures for their abstract methods and properties. C# provides keyword abstract to declare a method or property as abstract. Methods and properties that are abstract do not provide implementations attempting to do so is a
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.