04-objectspt3

04-objectspt3 - Object Fundamentals Part Three Kenneth M....

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Object Fundamentals Part Three Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 — Lecture 4 — 09/03/2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 2
Background image of page 2
Overview • Delegation • HAS-A • Inheritance • IS-A • Polymorphism • message passing • polymorphic arguments and return types • Interfaces • Abstract Classes • Object Identity • Code Examples 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Delegation (I) • When designing a class, there are three ways to handle an incoming message • Handle message by implementing code in a method • Let the class’s superclass handle the request via inheritance • Pass the request to another object (delegation) • Note: goes hand in hand with composition (not to be confused with aggregation/composition which is a design concept) • You compose one object out of others • The host object delegates requests to its internal objects 4
Background image of page 4
Delegation (II) • Delegation is employed when some other class already exists to handle a request that might be made on the class being designed • The host class simply creates a private instance of the helper class and sends messages to it when appropriate • As such, delegation is often referred to as a “HAS-A” relationship • A Car object HAS-A Engine object 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
• Here is an example of a class delegating a responsibility to another class • Grocery List has an attribute called items and it delegates all of its work- related tasks (storing/enumerating items) to it. Simple Example (I) 6 class GroceryList(object): def __init__(self, items): self.items = items def add_item(self, item): self.items.append(item) def print_items(self): for i, item in enumerate(self.items): print("{0}. {1}".format(i+1, item))
Background image of page 6
• We can create a GroceryList using a python list object like this: • Now imagine, we no longer liked the capabilities of the default list and we wanted to switch to another class, for example, a list that keeps its items sorted • This line is creating a new sorted list, passing in the current set of items, and setting GroceryList.items to the new sorted list. This is an example of a delegation relationship changing at runtime. Simple Example (II) 7 my_list = GroceryList([]) my_list.items = SortedList(my_list.items)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Delegation (III) • Advantages • Delegation is dynamic (not static) • delegation relationships can change at run-time • Not tied to inheritance (indeed, considered much more fexible) • In languages that support only single inheritance this is important! 8
Background image of page 8
Inheritance (I) • Inheritance is a mechanism for sharing (public/protected) features between classes • A class deFnes a type. • A superclass is a
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

04-objectspt3 - Object Fundamentals Part Three Kenneth M....

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online