DOC PREVIEW
CU-Boulder CSCI 5448 - MORE OO FUNDAMENTALS

This preview shows page 1-2-3-21-22-23-43-44-45 out of 45 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

© Kenneth M. Anderson, 2011MORE OO FUNDAMENTALSCSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGNLECTURE 4 — 01/20/20111Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Goals of the LectureContinue a review of fundamental object-oriented concepts2Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Overview of OO FundamentalsDelegationHAS-AMore on InheritanceIS-AMore on Polymorphismmessage passingpolymorphic arguments and return typesInterfacesAbstract ClassesObject Identity3Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Delegation (I)When designing a class, there are four ways to handle an incoming messageHandle message by implementing code in a methodLet the class’s superclass handle the request via inheritancePass the request to another object (delegation)some combination of the previous three4Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Delegation (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 object5Wednesday, January 19, 2011© Kenneth M. Anderson, 20116import java.util.List;1import java.util.LinkedList;23public class GroceryList {45 private List<String> items;67 public GroceryList() {8 items = new LinkedList<String>();9 }1011 public void addItem(String item) {12 items.add(item);13 }1415 public void removeItem(String item) {16 items.remove(item);17 }1819 public String toString() {20 String result = "Grocery List\n------------\n\n";21 int index = 1;22 for (String item: items) {23 result += String.format("%3d. %s", index++, item) + "\n";24 }25 return result;26 }2728}2930GroceryList delegates all of its work to Java’s LinkedList class (which it accesses via the List interface).Wednesday, January 19, 2011© Kenneth M. Anderson, 20117public class Test {12 public static void main(String[] args) {3 GroceryList g = new GroceryList();4 g.addItem("Granola");5 g.addItem("Milk");6 g.addItem("Eggs");7 System.out.println("" + g);8 g.removeItem("Milk");9 System.out.println("" + g);10 }1112}1314With the delegation, I get a nice abstraction in my client code. I can create grocery lists, add and remove items and get a printout of the current state of the list.Wednesday, January 19, 2011© Kenneth M. Anderson, 20118import java.util.List;1import java.util.LinkedList;23public class TestWithout {45 public static void printList(List<String> items) {6 System.out.println("Grocery List");7 System.out.println("------------\n");8 int index = 1;9 for (String item : items) {10 System.out.println(String.format("%3d. %s", index++, item));11 }12 System.out.println();13 }1415 public static void main(String[] args) {16 List<String> g = new LinkedList<String>();17 g.add("Granola");18 g.add("Milk");19 g.add("Eggs");20 printList(g);21 g.remove("Milk");22 printList(g);23 }2425}2627Without delegation, I get less abstraction. I’m using the List interface directly with its method names and I have to create a static method to handle the printing of the list rather than using toString().Wednesday, January 19, 2011© Kenneth M. Anderson, 20119Delegation (III)Now, the two programs (with delegation and without delegation) produce exactly the same outputSo, do we care which method we use?Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Delegation (IV)Benefits of DelegationBetter abstractionLess code in classes we write ourselvesWe can change delegation relationships at runtime!Unlike inheritance relationships; Imagine if we had created GroceryList as a subclass of LinkedList (*shudder*)Why? Because GroceryList IS-NOT-A LinkedList10Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Delegation (V)Changing delegation relationships at run-timeA class can use a set at run-timeSet<String> items = new HashSet<String>();If the class suddenly needs to be sorted, it can do thisitems = new TreeSet<String>(items);We have changed the delegation to an entirely new object at run-time and now the items are sortedIn both cases, the type of items is Set<String> and we get the correct behavior via polymorphism11Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Delegation (VI)SummaryDon’t re-invent the wheel… delegate!Delegation is dynamic (not static)delegation relationships can change at run-timeNot tied to inheritanceindeed, considered much more flexible; In languages that support only single inheritance this is important!12Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Inheritance (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 subclass restricts the legal values of its superclassComponent → Container → Control → Button → Checkbox13Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Inheritance (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 B is true14Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Inheritance (III)Good 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 WindowThe latter might actually be tried by an inexperienced designer who wants to display each Dog object in its own separate windowThis is known as implementation inheritance; it is considered poor design and something to be avoided15Wednesday, January 19, 2011© Kenneth M. Anderson, 2011Inheritance (IV)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 likely contains a lot of duplicated codeThe second uses inheritance and requires less codeeven though it has more classes than the former16Wednesday, January 19, 2011© Kenneth M. Anderson,


View Full Document

CU-Boulder CSCI 5448 - MORE OO FUNDAMENTALS

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download MORE OO FUNDAMENTALS
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 MORE OO FUNDAMENTALS 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 MORE OO FUNDAMENTALS 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?