Unformatted text preview:

Object FundamentalsPart ThreeKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/6448 — Lecture 4 — 09/06/20071Friday, September 7, 2007Lecture GoalsContinue our tour of the basic concepts, terminology, and notations for object-oriented analysis, design, and programmingSome material for this lecture is drawn from Head First Java by Sierra & Bates, © O'Reilly, 20032Friday, September 7, 2007OverviewDelegationHAS-AInheritanceIS-APolymorphismmessage passingpolymorphic arguments and return typesInterfacesAbstract ClassesObject IdentityCode Examples3Friday, September 7, 2007Delegation (I)When designing a class, developers have three ways to deal with an incoming requestHandle request by implementing code in a methodLet the class’s superclass handle the requestThis is called inheritance, discussed nextDelegate the request to another object (delegation)4Friday, September 7, 2007Delegation (II)Delegation is employed when some other class already exists to handle a request that might be made on the class being designedThe host class simply creates a private instance of the helper class and sends messages to it when appropriateAs such, delegation is often referred to as a “HAS-A” relationshipA Car object HAS-A Engine object5Friday, September 7, 2007Delegation (III)AdvantagesDelegation is dynamic (not static)delegation relationships can change at run-timeNot tied to inheritanceIn languages that support only single inheritance this is important!6Friday, September 7, 2007Inheritance (I)Inheritance is a mechanism for sharing (public/protected) features between classesA class defines a type. A superclass is a more generic instance of that type. A subclass is a more specific instance of that type.A subtype typically restricts the legal values of its superclassReal Numbers → Integers → Positive IntegersComponent → Container → Control → Button → Checkbox7Friday, September 7, 2007Inheritance (II)Subclasses have an “IS-A” relationship with their superclassA Hippo IS-A Animal makes sense while the reverse does notIS-A relationships are transitiveIf D is a subclass of C and C is a subclass of B, then D IS-A C and D IS-A B are both trueGood OO design strives to make sure that all IS-A relationships in a software system “make sense”Consider Dog IS-A Canine vs. Dog IS-A Window8Friday, September 7, 2007Inheritance (III)Inheritance enables significant code reuse since subclasses gain access to the code defined in their ancestorsThe next two slides show two ways of creating a set of classes modeling various types of AnimalsThe first uses no inheritance and most likely would contain a lot of duplicated codeThe second uses inheritance and would most likely require less code than the first exampleeven though it has more classes than the former9Friday, September 7, 2007Animals (No Inheritance)LionmakeNoise()roam()sleep()CatmakeNoise()roam()sleep()TigermakeNoise()roam()sleep()HippomakeNoise()roam()sleep()ElephantmakeNoise()roam()sleep()RhinomakeNoise()roam()sleep()DogmakeNoise()roam()sleep()WolfmakeNoise()roam()sleep()10Friday, September 7, 2007Animals (With Inheritance)CatmakeNoise()TigermakeNoise()RhinomakeNoise()Animalsleep()Felineroam()Canineroam()Pachydermroam()WolfmakeNoise()WolfDogmakeNoise()LionmakeNoise()ElephantmakeNoise()HippomakeNoise()11Friday, September 7, 2007Code MetricsIndeed, I coded these two examples and discoveredwithout inheritance: 9 files, 200 lines of codewith inheritance: 13 files, 167 lines of codeapproximately a 15% savings in this simple example12Friday, September 7, 2007Inheritance (IV)An important aspect of inheritance is substitutabilitySince a subclass can exhibit all of the behavior of its superclass, it can be used anywhere an instance of its superclass is usedThe textbook describes this as polymorphism (more on that in a moment)Furthermore, subclasses can add additional behaviors that make sense for it and override behaviors provided by the superclass, altering them to suit its needsThis is both powerful AND dangerousWhy? Stay tuned for the answer…13Friday, September 7, 2007Polymorphism (I)Object-Oriented programming languages support polymorphism (“many forms”)In practice, this allows code to be written with respect to the root of an inheritance hierarchy and function correctly if applied to an instance of one of its subclasses14Friday, September 7, 2007Polymorphism (II)Message Passing vs. Method InvocationWith polymorphism, a message ostensibly sent to a superclass, may be handled by a subclassCompare thisAnimal a = new Animal()a.sleep() // sleep() in Animal calledwith thisAnimal a = new Lion()a.sleep() // sleep() in Lion called15Friday, September 7, 2007Polymorphism ExampleWithout polymorphism, the code on the right only calls methods in the Animals classWith polymorphisma.roam() invokes Feline.roam()a.makeNoise() invokes Lion.makeNoise()Animalsleep()roam()makeNoise()Felineroam()LionmakeNoise()Animal a = new Lion()a.makeNoise();a.roam();a.sleep();16Friday, September 7, 2007Why is this important?Polymorphism allows us to write very abstract code that is robust with respect to the creation of new subclassesFor instancepublic void goToSleep(Animal[] zoo) { for (int i = 0; i < zoo.length; i++) { zoo[i].sleep(); }}17Friday, September 7, 2007Importance (II)In the previous codewe don’t care what type of animals are contained in the arraywe just call sleep() and get the correct behavior for each different type of animalIndeed, if a new subclass of animal is createdthe above code still functions correctly ANDit doesn’t need to be recompiledIt only cares about Animal, not its subclasses18Friday, September 7, 2007Importance (III)We can view a class’s public methods as establishing a contract that it and its subclasses promise to keepif we “code to the (root) contract”, we can create very robust and easy to maintain software systemsThis perspective is known as “design by contract”19Friday, September 7, 2007Importance (IV)Earlier, we referred to method overloading as “powerful AND dangerous”The danger comes from the possibility that a subclass may change the behavior of a method such that it no longer follows the contract established by a superclasssuch a change will break previously abstract and robust code20Friday, September 7, 2007Importance (V)Consider what would happen if an Animal subclass overrides the sleep() method to make its instances flee from a predator or eat a mealOur goToSleep() method would no


View Full Document

CU-Boulder CSCI 6448 - Object Fundamentals Part Three

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Object Fundamentals Part Three
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object Fundamentals Part Three and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object Fundamentals Part Three 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?