Aggregate Classes Notes Supplement CS 1054 Introduction to

  • Slides: 9
Download presentation
Aggregate Classes Notes Supplement CS 1054 Introduction to Programming in Java Spring 2008 6/22/2005

Aggregate Classes Notes Supplement CS 1054 Introduction to Programming in Java Spring 2008 6/22/2005

Class Relationships n n n More complex programs require multiple classes It is typical

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 Example of a class relationship: Composition Aggregate Classes 2

Object Composition n n Objects can be composed of other objects Have references to

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 an aggregation Aggregate Classes 3

Bank Example • A Bank encapsulates a set of Bank. Account objects • What’s

Bank Example • 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 deposit( “john”, 200 ) get. Balance( “marsha”) Aggregate Classes 4

Bank and Bank. Account double balance 1000 0 Bank. Account john Bank. Account marsha

Bank and Bank. Account double balance 1000 0 Bank. Account john Bank. Account marsha Bank. Account double balance 2000 0 Aggregate Classes 5

Object Composition in Java public class Bank { private Bank. Account john; private Bank.

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, double amt) { if ( name. equals( “john” ) ) john. deposit( amt ); . . . } Aggregate Classes 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 6

Object Interaction Bank deposit( “john”, 200 ) Bank. Account john Bank. Account deposit( 200

Object Interaction Bank deposit( “john”, 200 ) Bank. Account john Bank. Account deposit( 200 ) balance 1000 0 Bank. Account marsha Bank. Account balance 2000 0 Calling deposit on the Bank object causes deposit to be called on a Bank. Account object Aggregate Classes 7

The whole manages its parts n n n In effect, Bank (the aggregate class)

The whole manages its parts n n n In effect, Bank (the aggregate class) 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! Aggregate Classes 8

Chapter 7 preview n The aggregate class can contain an unlimited number of objects

Chapter 7 preview n The aggregate class can contain an unlimited number of objects that it manages n n Using arrays or Array. Lists For the Bank class, we will add a method that creates a new Bank. Account object within the Bank object Aggregate Classes 9