Download Object-Oriented Programming: Understanding Objects, Classes, and Relationships - Prof. Ken and more Study notes Computer Science in PDF only on Docsity! Object Fundamentals Part Two Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 — Lecture 3 — 09/02/2008 1Tuesday, September 2, 2008 Lecture Goals • Continue our tour of the basic concepts, terminology, and notations for object-oriented analysis, design, and programming • Some material for this lecture is drawn from Head First Java by Sierra & Bates, © O'Reilly, 2003 • But first! • Check out <http://www.google.com/googlebooks/chrome/index.html/> • for a unique take on documenting design decisions: comic book format • Google, one of the premier Internet companies, decided to announce its new Web browser by creating a comic book and sending it to people via postal mail (aka snail mail)!!! 2Tuesday, September 2, 2008 Objects (II) • Objects interact by sending messages to one another • Object A sends a message to Object B to request that it perform a task • When the task is complete, B may pass a value back to A • Note: sometimes A == B • that is, an object can send a message to itself • Sometimes messages can be rerouted; invoking a method defined in class A may be rerouted to an overridden version of that method in subclass B • And, invoking a method on an object of class B may invoke an inherited version of that method defined by superclass A 5Tuesday, September 2, 2008 Objects (III) • In response to a message, an object may • update its internal state • retrieve a value from its internal state • create a new object (or set of objects) • delegate part or all of the task to some other object • As a result, objects can be viewed as members of various object networks • Objects in an object network (aka collaboration) work together to perform a task for their host application 6Tuesday, September 2, 2008 Objects (IV) • UML notation • Objects are drawn as rectangles with their names and types underlined • Ken : Person • The name of an object is optional. The type, however, is required • : Person • Note: the colon is not optional. It’s another clue that you are talking about an object, not a class 7Tuesday, September 2, 2008 Classes (I) • A class is a blueprint for an object • The blueprint specifies the attributes (aka instance variables) and methods of the class • attributes are things an object of that class knows • methods are things an object of that class does • An object is instantiated (created) from the description provided by its class • Thus, objects are often called instances 10Tuesday, September 2, 2008 Classes (II) • An object of a class has its own values for the attributes of its class • For instance, two objects of the Person class can have different values for the name attribute • In general, each object shares the implementation of a class’s methods and thus behave similarly • When a class is defined, its developer provides an implementation for each of its methods • Thus, object A and B of type Person each share the same implementation of the sleep() method 11Tuesday, September 2, 2008 Classes (III) • Classes can define “class wide” (aka static) attributes and methods • A static attribute is shared among a class’s objects • That is, all objects of that class can read/write the static attribute • A static method does not have to be accessed via an object; you invoke static methods directly on a class • Static methods are often used to implement the notion of “library” in OO languages; it doesn’t make sense to have multiple instances of a Math class, each with their own sin() method • We will see uses for static attributes and methods throughout the semester 12Tuesday, September 2, 2008 Relationships: Inheritance • Classes can be related in various ways • One class can extend another (aka inheritance) • notation: an open triangle points to the superclass • As we learned last time, the subclass can add behaviors or override existing ones Animal location food type roam() eat() makeNoise() Hippo eat() makeNoise() submerge() 15Tuesday, September 2, 2008 Relationships: Association • One class can reference another (aka association) • notation: straight line • This notation is a graphical shorthand that each class contains an attribute whose type is the other class Zoo addAnimal() Hippo eat() makeNoise() Zoo addAnimal() Hippo ourHippo shortcut for Hippo eat() makeNoise() Zoo myZoo 16Tuesday, September 2, 2008 Multiplicity • Associations can indicate the number of instances involved in the relationship • this is known as multiplicity • An association with no markings is “one to one” • An association can also indicate directionality • Examples on next slide 17Tuesday, September 2, 2008 Relationships: whole-part • Associations can also convey semantic information about themselves • In particular, aggregations indicate that one object contains a set of other objects • think of it as a whole-part relationship between • a class representing a group of components • a class representing the components • Notation: aggregation is indicated with a white diamond attached to the class playing the container role 20Tuesday, September 2, 2008 Example: Aggregation Composition Book Section Chapter Aggregation Crate Bottle Composition will be defined on the next slide Note: aggregation and composition relationships change the default multiplicity of associations; instead of “one to one”, you should assume “one to many” * 21Tuesday, September 2, 2008 Semantics of Aggregation • Aggregation relationships are transitive • if A contains B and B contains C, then A contains C • Aggregation relationships are asymmetric • If A contains B, then B does not contain A • A variant of aggregation is composition which adds the property of existence dependency • if A composes B, then if A is deleted, B is deleted • Composition relationships are shown with a black diamond attached to the composing class 22Tuesday, September 2, 2008 Relationships: Interfaces • A class can indicate that it implements an interface • An interface is a type of class definition in which only method signatures are defined • A class implementing an interface provides method bodies for each defined method signature in that interface • This allows a class to play different roles, each role providing a different set of services • These roles are then independent of the class’s inheritance relationships • Other classes can then access a class via its interface • This is indicated via a “ball and socket” notation 25Tuesday, September 2, 2008 Example: Interfaces Dog location food type roam() eat() makeNoise() Pet Dog location food type roam() eat() makeNoise() Pet Person 26Tuesday, September 2, 2008 Class Summary • Classes are blue prints used to create objects • Classes can participate in multiple relationship types • inheritance • association • associations have multiplicity • aggregation/composition • qualification • Interfaces 27Tuesday, September 2, 2008 Graphically Animal location food type roam() eat() makeNoise() Hippo eat() makeNoise() submerge() List head elements findElement() removeElement() addElement() Inheriting from List in this way is bad, because “Hippo IS-A List” is FALSE A Hippo is NOT a special type of List Instead... 30Tuesday, September 2, 2008 Do This What’s the Difference? Animal location food type roam() eat() makeNoise() Hippo eat() makeNoise() submerge() List head elements findElement() removeElement() addElement() 31Tuesday, September 2, 2008 Another Problem C A B D What’s wrong with this? Hint: think about what might happen when you create an instance of D Fortunately: Python gets it right! See example code. 32Tuesday, September 2, 2008 35 Multiple Inheritance • So, is there a valid use of multiple inheritance? • Yes, sub-typing for combination • It is used to define a new class that is a special type of two other classes where those two base classes are from different domains • In such cases, the derived class can then legally combine data and behavior from the two different base classes in a way that makes semantic sense 35Tuesday, September 2, 2008 36 Multiple Inheritance Example Is a wooden door a special type of door? Yes Is a door part of a wooden door? No Is a wooden door a special type of wooden object? Yes Is a wooden object part of a door? No Is a wooden object a special type of door? No Is a door a special type of wooden object? No All Heuristics Pass! DoorWoodenObject WoodenDoor 36Tuesday, September 2, 2008 Coming Up Next • Lecture 4: Object Fundamentals, Part 3 • Lecture 5: Great Software • Read Chapter 1 of the OO A&D book 37Tuesday, September 2, 2008