Class Relationships and Object Interaction 882005 Class Relationships
Class Relationships and Object Interaction 8/8/2005
Class Relationships n n n 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 n n There is a class relationship between A and B Examples of class relationships n n 8/8/2005 Composition or Aggregation Association Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 2
Object Composition n n 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 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 3
Encapsulation n The idea of “hiding” the implementation of the functionality n n n 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 … n n users don’t need to know Data should be hidden with the object that it belongs to n n 8/8/2005 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 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 4
Bank Example n n n A Bank encapsulates a set of Bank. Account objects What’s important is the external interface Users don’t need to know what goes on inside the Bank withdraw( “john”, 200 ) get. Balance( “marsha”) 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 5
Bank and Bank. Account int balance Bank 1000 0 Bank. Account john Bank. Account marsha Bank. Account int balance 2000 0 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 6
Object Composition in Java public class Bank { private Bank. Account john; private Bank. Account marsha; public Bank() { john = new Bank. Account( 1000 ); marsha = new Bank. Account( 2000 ); }. . . public void deposit( String name, int amt) { if ( name. equals( “john” ) ) john. deposit( amt ); . . . } 8/8/2005 There are Bank. Account fields in Bank The fields are instantiated in Bank’s constructor Bank has its own deposit method that calls Bank. Account’s deposit method on the appropriate object Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 7
Object Interaction Calling deposit on the Bank object causes deposit to be called on a Bank. Account object Bank deposit( “john”, 200 ) Bank. Account john Bank. Account deposit( 200 ) int balance 1000 0 Bank. Account marsha Bank. Account int balance 2000 0 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 8
The whole manages its parts n n n In effect, Bank is a manager of Bank. Accounts Transactions are carried out through the Bank object but ultimately uses/affects a Bank. Account object The one calling Bank’s methods does not even need to know about the Bank. Account class this is exactly what encapsulation is about! 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 9
Object Association n Association: a weaker kind of relationship Unlike in the case of composition or aggregation, the creation or existence of one object does not depend on another Examples: n n n 8/8/2005 Borrower and Book in a library system Student, Class, Teacher in a university system Water. Tank and Faucet Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10
Water. Tank-Faucet Example n n n A Water. Tank object has methods that cause it to be filled up with water or to dispense water A Faucet object is connected to a Water. Tank and has methods to dispense or drain water Faucet needs a way to connect/associate to a Water. Tank object n 8/8/2005 Note: we can connect several faucets to a single water tank Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 11
Water. Tank-Faucet Association n Option 1: create Water. Tank object, create Faucet object(s), and call a method on Faucet: w = new Water. Tank(); f 1 = new Faucet(); f 2 = new Faucet(); f 1. connect( w ); f 2. connect( w ); n Option 2: Faucet’s constructor has a Water. Tank parameter w = new Water. Tank(); f 1 = new Faucet( w ); f 2 = new Faucet( w ); 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 12
Water. Tank and Faucet f 1: Faucet Water. Tank tank Water. Tank double water. Left 100. 0 f 2: Faucet Water. Tank tank 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 13
Object Association in Java public class Faucet { private Water. Tank tank; public Faucet( Water. Tank w ) { tank = w; }. . . public void connect( Water. Tank w ) { tank = w; }. . . } 8/8/2005 The association is represented by a Water. Tank field The field can be set in the constructor… …or in a method Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 14
Object Interaction dispense( 20. 0 ) f 1: Faucet Water. Tank tank dispense( 20. 0 ) Water. Tank double water. Left flush() 8/8/2005 f 2: Faucet Water. Tank tank 100. 0 dispense( 80. 0 ) Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 15
Object Interaction public class Faucet { private Water. Tank tank; public Faucet( Water. Tank w ) { tank = w; } public void dispense( double amt ) { tank. dispense( amt ); } public void flush() { tank. dispense( tank. get. Water. Left() ); } } 8/8/2005 public class Water. Tank { private double water. Left = 0; . . . public void fill. Tank(). . . public void dispense( double amt ) { water. Left -= amt; } public double get. Water. Left() { return water. Left; } } Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 16
An Integrated Example n n n 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 n n Multiple cashiers are associated to the Manager object The Manager object aggregates Product objects (where prices and stock levels are stored) 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 17
An Integrated Example c 1: Cashier Manager Product appples Product oranges c 2: Cashier Transactions are carried out through the Cashier objects 8/8/2005 Product pomelos Prices are checked and purchase requests are made thru the Manager object apples: Product oranges: Product pomelos: Product objects may be updated as a result Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 18
Summary n n n In Java, a program is a collection of interacting objects Programmers may develop multiple classes for these objects The classes are related by n n n Composition/Aggregation Association Later in the semester, we will introduce another relationship: Inheritance 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 19
- Slides: 19