Chapter 9 A Second Look at Classes and Objects - 3 Aggregation.

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Mutable, Immutable, and Cloneable Objects Chapter 15.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Chapter 6: A First Look at Classes
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
More C++ Features True object initialisation
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 9 – Part Tony Gaddis Modified by Elizabeth Adams.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 3 Class Diagrams. 2 Outline Class Basics Class Basics Classes Classes Association Association Multiplicity Multiplicity Inheritance Inheritance.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
A Second Look At Classes And Objects Chapter 9. Static Class Members When a value is stored in a static field, it is not in an object of the class. In.
Const declares immutable value void Test(char* t) { t++;// OK, address is not const t[0] = 'a'; // OK, char is not const } void Test2(const char* t) {
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Topics Instance variables, set and get methods Encapsulation
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Chapter 9 A Second Look at Classes and Objects - 2.
Definition and Application of Binary Trees
Chapter 14: More About Classes.
Programming Logic and Design Seventh Edition
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Introduction to Classes and Objects
Examples of Classes & Objects
Some Eclipse shortcuts
Chapter 9 More on Objects and Classes
Java Primer 1: Types, Classes and Operators
Introduction to Classes
A Second Look at Classes and Objects - 2
Chapter 14: More About Classes.
Object Oriented Programming
Class Structure 16-Nov-18.
Introduction to Classes
CSCI 1260 – Lecture 2: Instantiation, Aggregation, and Composition
Classes & Objects: Examples
Object-Oriented Programming Using C++ Second Edition
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Today’s topics UML Diagramming review of terms
Classes, Objects, Methods and Strings
Chapter 8 Classes User-Defined Classes and ADTs
Class Structure 25-Feb-19.
Object Oriented Programming Review
Dr. Sampath Jayarathna Cal Poly Pomona
Making a copy of an object: shallow and deep copy
SPL – PS3 C++ Classes.
Presentation transcript:

Chapter 9 A Second Look at Classes and Objects - 3 Aggregation

2 Contents I. Aggregation II. Security Issues with Aggregate Classes III. Avoid Using null References

3 I. Aggregation Aggregation occurs when an instance of a class is a field in another class. In real life, objects are frequently made of other objects. A house is made of door objects window Objects wall objects … It is the combination of all these objects that makes a house object.

4 The Course Class An example: We need an object to represent a course that you are taking in college. A Course class hold the following information: The course name The instructor's last name, first name, and office number The textbook's title, author, and publisher We can put fields for each of these items in the Course class. However, a good design principle is to separate related items into their own classes.

5 The Course Class An Instructor class holds the instructor-related data: The instructor's last name, first name, and office number A TextBook class holds the textbook-related data: The textbook's title, author, and publisher A Course class holds: The course name An Instructor object A TextBook object

6 The Course Class UML class diagram for the Instructor class and TextBook class:

7 The Course Class UML diagram for the Course class. Notice that the Course class has an Instructor object and a TextBook object.

8 The Course Class Making an instance of one class a field in another class is called object aggregation. The word aggregation means “a whole which is made of constituent parts”. The Course class is an aggregate class because it is made of constituent objects. “Has a” relationship When an instance of one class is a member of another class, it is said that there is a “has a” relationship between class.

9 The Course Class The “has a” relationship is sometimes called a whole-part relationship because one object is part of a greater whole. The relationships that exist among the Course, Instructor, and TextBook classes can be described as follows: The course has an instructor. The course has a textbook.

10 The Course Class Aggregation in UML diagram: Aggregation is shown in UML class diagram by connecting two classes with a line that has an open diamond at one end. The diamond is closet to the class that is the aggregate.

11 The Course Class

12

13

14

15 The Course Class

16 The Course Class

17 II. Security Issues with Aggregate Classes When writing an aggregate class, we should be careful not to unintentionally create “security holes” that can allow code outside to modify private data inside the class. Perform Deep Copies When Creating Field Objects: Return Copies of Field Object, Not the Originals

18 Perform Deep Copies When Creating Field Objects An aggregate object contains references to other objects. When we make a copy of the aggregate object, it is important that you also make copies of the objects it references. This is known as a deep copy. If we make a copy of an aggregate object, but only make a reference copy of the objects it references, then we have performed a shallow copy.

19 Perform Deep Copies When Creating Field Objects Deep Copies public Course(String name, Instructor instr, TextBook text) { //Assign the courseName courseName = name; //Create a new Instructor object, passing //instr as an argument to the copy instructor. instructor = new Instructor(instr); //Create a new TextBook object, passing //text as an argument to the copy instructor. textBook = new TextBook(text); }

20 Perform Deep Copies When Creating Field Objects Bad constructor in the aggregation context public Course(String name, Instructor instr, TextBook text) { //Assign the courseName courseName = name; instructor = instr; textBook = text; } In this example, the instructor and textBook fields are merely assigned the address of the objects passed into the constructor.

21 Perform Deep Copies When Creating Field Objects This can cause problems because there may be variables outside the Course object that also contain references to these Instructor and TextBook objects. These outside variables would provide direct access to the Course object's private data. Why was a deep copy not also done for the courseName field? This is permissible because String objects are immutable. An immutable object does not provide a way to change its contents.

22 Return Copies of Field Object, Not the Originals When a method in the aggregate class returns a reference to a field object, return a reference to a copy of the field object. public Instructor getInstructor() { return new Instructor(instructor); } Bad method in the aggregation context public Instructor getInstructor() { return instructor; //WRONG! Causes a // security hole } Any variable that receives the address can then access the Instructor object. This means the code outside the Course object can change the values held by the Instructor object.

23 Return Copies of Field Object, Not the Originals Note: It is permissible to return a reference to a String object, even if the String object is a private field. This is because String objects are immutable.

24 III. Avoid Using null References By default, a reference variable that is an instance field is initialized to the value null. This value indicates that the variable does not reference an object. We cannot use it to perform an operation that would require the existence of an object. For example, a null reference variable cannot be used to call a method.

25

26 III. Avoid Using null References FullName name = new FullName(); //Get the length of the full name len = name.getLength(); This code will crash because the getLength method is called before the name object's fields are made to reference String objects.

27 III. Avoid Using null References One way to prevent the program from crashing: public int getLength() { int len = 0; if(lastName != null) len += lastName.length(); if(firstName != null) len += firstName.length(); if(middleName != null) len += middleName.length(); return len; }

28 III. Avoid Using null References Another way to handle this problem is to write a no-arg constructor that assigns values to the reference fields. public FullName() { lastName = “”; firstName = “”; middleName = “”; }