Class Relationships. In systems with multiple classes, it can become difficult to keep track of relationships  e.g. the Student class requires the Course.

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

Visual Basic: An Object Oriented Approach 2 – Designing Software Systems.
Inheritance Inheritance Reserved word protected Reserved word super
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Unified Modeling Language (UML)
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Engineering Software Development Process.
Unified Modeling Language
The Unified Modeling Language (UML) Class Diagrams.
Association & Aggregation Lecture-9. Vehicle Car Tyre Engine Bus Passenger.
Object Oriented Software Development
Object Oriented Design and UML
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Writing Classes (Chapter 4)
(C) 2009 J. M. Garrido1 Object Oriented Simulation with Java.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies – Extreme Programming Object-Oriented Design – CRC Cards - UML.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Inheritance Reading for this Lecture: L&L 6.4 – 6.5.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Computer Programs and Programming Languages What are low-level languages and high-level languages? High-level language Low-level language Machine-dependent.
(c) University of Washington01-1 CSC 143 Java Programming as Modeling Reading: Ch. 1-6.
An Introduction to the Unified Modeling Language
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
CS 151: Object-Oriented Design September 5 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
Static class members.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
ITEC324 Principle of CS III Chapter 2 (Horstmann’s Book) – Part 1 The Object-Oriented Design Process Hwajung Lee.
CSE 219 Computer Science III UML. UML Diagrams UML - Unified Modeling Language UML diagrams are used to design object-oriented software systems –represent.
Week101 APCS-AB: Java Miscellaneous Topics: Snippets we missed in Chapters 1-6 of book November 11, 2005.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited.
Unified Modeling Language (UML)
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Chapter 2 (Horstmann’s Book) – Part 1 The Object-Oriented Design Process Hwajung Lee.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
UML Diagrams and Software Testing Instructor: Scott Kristjanson CMPT 135 SFU Surrey, Spring 2016.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
COP 3330 Notes 4/13. Today’s Topics UML Class Diagrams.
Object-Orientated Analysis, Design and Programming
Unified Modeling Language
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
The Object-Oriented Thought Process Chapter 1
An Introduction to Inheritance
Chapter 4: Writing Classes
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Basics of OOP A class is the blueprint of an object.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
ITEC324 Principle of CS III
Object Oriented Programming
Presentation transcript:

Class Relationships

In systems with multiple classes, it can become difficult to keep track of relationships  e.g. the Student class requires the Course class to work There are many ways classes can interact  Details of the interactions are part of the design

Class Relationships Three of the most common relationships:  Dependency: A uses B  Aggregation: A has-a B  Inheritance: A is-a B Today: dependency and aggregation Inheritance later

Dependency It’s very common for one class to use another  methods use a class to temporarily store/manipulate information  an instance variable gets a new value by using another class  arguments are passed of another class type … any case where a class needs another class to compile or run

Dependency A dependency exists when one class relies on another in some way, usually by invoking the methods of the other We don't want numerous or complex dependencies among classes  in general – we want to minimize dependencies Nor do we want complex classes that don't depend on others A good design strikes the right balance

Dependency Some dependencies occur between objects of the same class A method of the class may accept an object of the same class as a parameter For example, the concat method of the String class takes as a parameter another String object str3 = str1.concat(str2); This drives home the idea that the service is being requested from a particular object

Dependency Example public class Coin { char face; \\ ‘h’ for heads, ‘t’ for tails …… contstructors, methods, etc…… public void flip() { Random rg = new Random();  DEPENDENCY! int rand = rg.nextInt(2); if(r==0) face = ‘h’; else face = ‘t’; }

Aggregation An aggregate is an object that is made up of other objects Therefore aggregation is a has-a relationship  A bike has a two wheels In software, an aggregate object contains references to other objects as instance data The aggregate object is defined in part by the objects that make it up This is a special kind of dependency – the aggregate usually relies on the objects that compose it

Aggregation Example public class Wheel { private int diameter; Wheel(int diameter) { this.diameter = diameter; } public int getDiameter() { return diameter; } public int getCircumference() { return Math.pi*diameter; }

Aggregation Example public class Bike { private Wheel frontwheel; private Wheel backwheel; ….other instance variables…. Bike(int frontsize, int ibacksize) { this.frontwheel = new Wheel(frontsize); this.backwheel = new Wheel(backsize); } …..other methods } -other methods may use wheel size… -for example to compute distance traveled -Note: A Bike cannot be created without Wheel objects

Graphical Representations UML – Unified Modelling Language  general methods to model/design/document software and other structures  commonly used to design object oriented systems The are several different types of UML diagrams  … including “class diagrams”

Class Diagrams Diagrams that are used to describe classes and class relationships Represents:  classes: instance variables, methods (and relevant types and arguments)  class relationships: dependency and aggregation…

Drawing a Class Some details occasionally omitted (for clarity) Not Java syntax – language independent

Class Relationships Other arrows illustrate different relationships You should be able to read these It is up to you if you find them useful in your own designs

Design and UML A full UML diagram gives a lot of information about the design  creating one requires very careful planning Probably too detailed for initial planning  could be done for low-level design or documentation  Often, details will change during implementation – but don’t plan it that way Our blackjack design isn’t finished yet….