Concepts 6 Generalization A relation between a general and a specialized element. The more special adds further properties. Inheritance A relation between a VXSHUFODVV and a VXEFODVV. Subclasses share properties of their superclasses. OOAD Part II Intro to inheritance BlueJ ex 5.1 The DoME project is a database for multimedia entertainment. Open DoME-v1. Draw class diagram. (Aggregate or composite?) Discuss extension with DVDs (etc.) OO and inheritance So far, we have discussed OO principles that are relatively easy to support also in non OO languages such as C, F77, etc. Now, we continue with inheritance, a key feature of OO. Not supported in non OO languages. NB: OOAD may still be used for designing, but implementation of OOAD is easier in OO languages.
BlueJ ex 5.3 Open DoME-v3. What is the difference between v2 and v3? Is the polymorphism useful here? Discuss extension with DVDs. BlueJ ex 5.2 Open DoME-v2. Draw class diagram. Discuss extension with DVD. Has anything been gained so far? Has anything been lost? Concepts 8 Concepts 7 Protected visibility may be specified for features in a superclass. Protected features are visible to subclasses. Remark: Protected visibility is often useful. However, it should be used with care, since it does violate information hiding. Polymorphism When homonymous messages to compatible objects of different classes may trigger a different behavior. In dynamic polymorphism, a message is assigned to a concrete method not at compile time, but during runtime. Dynamic binding is a prerequisite for dynamic polymorphism. Remark: Two objects of different subclasses to same superclass are compatible. Inheritance is often called the is-a relationship.
OOAD Regarding polymorphism: Usually, it refers to dynamic polymorphism. Sometimes, compile-time (static) polymorphism can be used instead. In the OO analysis, it is not important to distinguish between dynamic and static polymorphism. In the OO design, static polymorphism can (sometimes ) be chosen. This may be important for scientific programs, with high performance demands. BlueJ ex 5.4 Discuss the visibility of all inherited features of an Item. How does protected features of Item affect the Database? On simulations: BlueJ ex 6 intro Computers often used for (scientific) simulations A computer simulation tries to model a subset of the real world. Need to simplify Abstraction LV important. Benefit of simulations: experiments, what if questions, etc. (when real thing is too costly, or dangerous, or ) Example: Environmental simulation: effect of human activity on a habitat. Suppose a freeway thru a national park is planned: Will the animals be affected? OOAD Summary of inheritance so far. Benefits: Avoids code duplication Promotes reuse (beware ) Simplifies maintenance Simplifies extensions Promotes abstraction Note that inheritance is not DOZD\V suitable. Too complex inheritance hierarchies are difficult to understand. For time critical operations, it may degrade performance.
BlueJ ex 1.2, 5.4 Let us revisit the DoME project. Should the Item super class be abstract? Consider adding a Shape super class in the Shapes project. Should the Shape super class be abstract? BlueJ ex 6.1 Open foxes-and-rabbits-v1 project. Play around with it. (Is the simulation realistic?) Draw &5&FDUGV for classes Simulator, Rabbit and Fox. (A CRC card states the class, its responsibilities and its collaborators.) Compare CRC cards with class diagram. When may CRC cards be useful? Illustrate in detail how the associations between Simulator, Rabbit, and Fox is implemented. BlueJ ex 6.2 Of course, an abstract class Animal is appropriate. Discuss its benefits for Fox and Rabbit. Discuss its benefit for Hare. Discuss its benefits for Simulator. Is the dependency on Fox and Rabbit completely removed? What changes do we get in other classes? Concept 9 Abstract class An abstract class is intentionally incomplete; it lacks implementation of some methods. These methods are called abstract methods. No objects can be instantiated of an abstract class. Concrete class is a class which is not abstract. Note that (concrete) subclasses to abstract classes must implement the abstract methods.
OOAD summary part II We have discussed Generalization Inheritance Polymorphism Abstract classes Interfaces Inheritance is a key feature of OO. It is essential for polymorphism. In OOAD, it should model is-a dependencies. Concepts 10 Multiple inheritance is when a subclass inherits (directly) from more than one super class. Interface A class which only specifies abstract methods is an interface. Remarks: Multiple inheritance may cause trouble. The problem is when LPSOHPHQWDWLRQ is multiply inherited. Multiple inheritance of interfaces is OK, though. BlueJ ex 6.3 Discuss a Drawable interface. Consider classes Actor, Animal, Hunter, Fox, Rabbit, and Season. Discuss SimulatorView as a super class to AnimatedView and TextView. What if you want both an animated view and a text view? When do we use abstract classes and when do we use interfaces?