Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Slides:



Advertisements
Similar presentations
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Advertisements

Overview of Data Structures and Algorithms
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Programming Languages and Paradigms Object-Oriented Programming.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Introduction to Object-Oriented Programming
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object-Oriented Programming in C++
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Class Relationships and Object Interaction. 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Class Relationships and Object Interaction CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Arrays and Lists: Handling Infinite Data CS 21a: Introduction to Computing I First Semester,
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Class Relationships in C++
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Chapter 3: Using Methods, Classes, and Objects
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Building Java Programs
Building Java Programs
Writing Methods AP Computer Science A.
Lecture 2: Implementing ArrayIntList reading:
CS2011 Introduction to Programming I Objects and Classes
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Arrays and Array Lists CS 21a.
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester,

Class Relationships ► More complex programs require multiple classes ► It is typical for objects to have fields that refer to other objects ► In class A, there may be a field whose type is class B ► There is a class relationship between A and B ► Examples of class relationships ► Composition or Aggregation ► Association

Object Composition ► Objects can be composed of other objects ► Have references to "parts" of the class as fields of the class ► Objects can create instances of other objects ► Also called aggregation

Encapsulation ► The idea of "hiding" implementation details ► What’s more important is the interface ► Users don’t need to know how a method works, just that it’s there and it works ► Objects know how to handle themselves … ► users don’t need to know

Encapsulation ► Data should be hidden with the object that it belongs to ► Changes to data should be done via methods of object that contains the data ► Again … objects should know how to handle the data ► Allows the object’s programmer to change data representation ► This is why we make fields private

Bank Example ► A Bank encapsulates a set of BankAccount objects ► What’s important is the external interface ► Users don’t need to know what goes on inside the Bank, and Bank doesn’t need to know what goes on inside BankAccount getBalance( "marsha") withdraw( "john", 200 )

Bank and BankAccount BankAccount balance 1000 BankAccount balance 2000 Bank BankAccount john BankAccount marsha

Object Composition in Java public class Bank { private BankAccount john; private BankAccount marsha; public Bank() { john = new BankAccount( 1000 ); marsha = new BankAccount( 2000 ); } public void deposit(String name, double amt) { if ( name.equals( "john" ) ) john.deposit( amt );... }... } There are BankAccount fields in Bank The fields are instantiated in Bank’s constructor Bank has its own deposit method that calls BankAccount’s deposit method on the appropriate object

Using a Bank Object Bank b = new Bank(); b.deposit( "john", 200 ); b.withdraw( "marsha", 100 ); System.out.println( b.getBalance( "john" ) ); System.out.println( b.getBalance( "marsha" ) ); Prints:

Object Interaction BankAccount balance 1000 BankAccount balance 2000 Bank BankAccount john BankAccount marsha deposit( "john", 200 ) deposit( 200 ) Calling deposit on the Bank object causes deposit to be called on a BankAccount object

The whole manages its parts ► In effect, Bank is a manager of BankAccounts ► Transactions are carried out through the Bank object but ultimately uses/affects a BankAccount object ► The one calling Bank’s methods does not even need to know about the BankAccount class  this is exactly what encapsulation is about!

Composition with Array(List)s ► An object can be composed of a fixed number of other objects ► A fixed number of fields can implement that composition ► But in general, an object may be composed of an arbitrary number of instances of another object

Composition with Array(List)s public class Bank { private ArrayList accounts; public Bank() { accounts = new ArrayList (); } public void openAccount( String name, double init ) { accounts.add( new BankAccount(name, init) ); }... There is an ArrayList of BankAccount objects in Bank The ArrayList is instantiated in Bank’s constructor The actual BankAccounts are instantiated when they are needed.

Composition with Array(List)s... public void deposit( String name, double amt ) { BankAccount acct = find( name ); if( acct != null ) acct.deposit( amt ); }... } Deposit now requires a search for the correct BankAccount instance (done with a loop instead of hard- coded if-else)

Object Association ► Association: a weaker kind of relationship ► Examples: ► Borrower and Book in a library system ► Student, Class, Teacher in a university system ► WaterTank and Faucet

WaterTank-Faucet Example ► A WaterTank object has methods that cause it to be filled up with water or to dispense water ► A Faucet object is connected to a WaterTank and has methods to dispense or drain water ► Faucet needs a way to connect/associate to a WaterTank object ► Note: we can connect several faucets to a single water tank

WaterTank-Faucet Association ► Option 1: create WaterTank object, create Faucet object(s), and call a method on Faucet: w = new WaterTank(); f1 = new Faucet(); f2 = new Faucet(); f1.connect( w ); f2.connect( w ); ► Option 2: Faucet’s constructor has a WaterTank parameter w = new WaterTank(); f1 = new Faucet( w ); f2 = new Faucet( w );

WaterTank and Faucet f1: Faucet WaterTank tank f2: Faucet WaterTank tank WaterTank double waterLeft 100.0

Object Association in Java public class Faucet { private WaterTank tank; public Faucet( WaterTank w ) { tank = w; }... public void connect( WaterTank w ) { tank = w; }... } The association is represented by a WaterTank field The field can be set in the constructor… …or in a method

Object Interaction f1: Faucet WaterTank tank f2: Faucet WaterTank tank WaterTank double waterLeft dispense( 20.0 ) flush() dispense( 20.0 ) dispense( 80.0 )

Object Interaction public class Faucet { private WaterTank tank; public Faucet( WaterTank w ) { tank = w; } … public void dispense( double amt ) { tank.dispense( amt ); } public void flush() { tank.dispense( tank.getWaterLeft() ); } public class WaterTank { private double waterLeft = 0;... public void fillTank()... public void dispense( double amt ) { waterLeft = waterLeft - amt; } public double getWaterLeft() { return waterLeft; }

Using Faucet and WaterTank Objects WaterTank w = new WaterTank(); WaterTank x = new WaterTank(); w.fillTank(); // fills tank to capacity, say 100 gallons x.fillTank(); // fills tank to capacity, say 100 gallons Faucet f1 = new Faucet( w ); Faucet f2 = new Faucet( w ); f1.dispense( 20 ); f2.flush(); f1.connect( x ); f1.dispense( 50 ); System.out.println( w.getWaterLeft() ); System.out.println( x.getWaterLeft() ); Prints: 0 50

Composition versus Association ► In both cases, there appears to be a one- to-many relationship ► One Bank, many BankAccounts ► One WaterTank, many Faucets ► Why is it not correct to say that the WaterTank is composed of Faucets?

Composition versus Association ► Composition: The one manages the many ► Implies whole-part relationship ► The body is composed of cells, but the cells do not need to be aware of the body ► Association: The many use the one ► Does not imply a whole-part relationship ► Many parasites can feed off a host, but the host does not need to be aware of the parasites

Composition versus Association ► Composition: the whole is dependent on the parts ► Association: there is no such dependency

Composition ► It doesn’t make sense to have Banks if there is no concept of BankAccount (the class) ► Although it’s perfectly okay to start a Bank without any BankAccounts (the instances) yet ► In other words, if BankAccount is removed from the system, it no longer makes sense to retain Bank.

Association ► It still makes sense to have a WaterTank even if Faucets were never invented ► Faucets don’t need to be connected to WaterTanks. They can be connected to pipes, for example. ► In other words, if Faucet is removed from the system, it’s perfectly fine to retain WaterTank, and vice versa.

An Integrated Example ► Grocery environment ► Products are stocked and sold in the grocery ► Cashiers are front-end objects that carry out a sale through a back-end price-and-stock Manager object ► Multiple cashiers are associated to the Manager object ► The Manager object aggregates Product objects (where prices and stock levels are stored)

Grocery Example c1: Cashier c2: Cashier Manager product apples product oranges product pomelos apples: Product oranges: Product pomelos: Product Transactions are carried out through the Cashier objects Product objects may be updated as a result Prices are checked and purchase requests are made thru the Manager object

Exercise ► Can you identify the class relationships present in the Project 1 solution?