Class Relationships and Object Interaction CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester,
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. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
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.
Java Programming Review (Part I) Enterprise Systems Programming.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
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.
Loops and Iteration for Statements, while Statements and do-while Statements.
Object-Oriented Programming in C++
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Class Relationships and Object Interaction. 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Programming Languages and Paradigms Programming C++ Classes.
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.
UML Class Diagrams 1 These lecture slides are copyright (C) Marty Stepp, They may not be rehosted, sold, or modified without expressed permission.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Data Structures and Java CS /14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Java Basics Variables, Expressions, Statements, etc. CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Packages. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Class Relationships in C++
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Chapter 3: Using Methods, Classes, and Objects
Event Handling CS 21a: Introduction to Computing I
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
CS2011 Introduction to Programming I Objects and Classes
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Outline Anatomy of a Class Encapsulation Anatomy of a Method
AN INTRODUCTION TO OBJECTS AND CLASSES
Programming Languages and Paradigms
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Class Relationships and Object Interaction CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 2 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 3 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 4 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 5 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 6 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 getBalance( “marsha”) withdraw( “john”, 200 )

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 7 Bank and BankAccount BankAccount balance BankAccount balance Bank BankAccount john BankAccount marsha

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 8 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 9 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:

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 10 Object Interaction BankAccount balance BankAccount balance 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 11 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!

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 12 Object Association 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: Borrower and Book in a library system Student, Class, Teacher in a university system WaterTank and Faucet

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 13 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 14 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 );

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 15 WaterTank and Faucet f2:Faucet WaterTank tank WaterTank double waterLeft f1:Faucet WaterTank tank

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 16 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 17 Object Interaction f2:Faucet WaterTank tank WaterTank double waterLeft f1:Faucet WaterTank tank dispense( 20.0 ) flush() dispense( 20.0 ) dispense( 80.0 )

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 18 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 = water - amt; } public double getWaterLeft() { return waterLeft; }

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 19 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 20 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)

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 21 Grocery example c1:Cashierc2:Cashier apples: Product Manager Product appples Product oranges Product pomelos pomelos: Product oranges: 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

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved Interaction Slide 22 Summary In Java, a program is a collection of interacting objects Programmers may develop multiple classes for these objects The classes are related by Composition/Aggregation Association Later in the semester, we will introduce another relationship: Inheritance