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.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Association relationship We’ve seen one implementation of “knows a”: public class Dog.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Now What? What if we want to talk to something that was created somewhere else? E.g.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Fall 2005CSE 115/503 Introduction to Computer Science I1 Reminder Check the course web site on a regular basis:
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
Where do objects come from? Objects are instances of classes We instantiate classes: –e.g.new chapter1.Terrarium() –There are three parts to this expression:
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Announcements Attendance sheet is going around – be sure you sign it! First part of.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Fall 2005CSE 115/503 Introduction to Computer Science I1 Association Also called “knows a”. A relationship of knowing (e.g. Dog-Collar as opposed to Dog-Tail)
Week 5 Recap CSE 115 Spring Composition Informally called “has a” Represented in UML with a diamond- headed arc In code: Declare an instance variable.
CSE 115 Week 3 January 28 – February 1, Monday Announcements Software Installation Fest: 2/5 and 2/6 4pm – 7pm in Baldy 21 Software Installation.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Announcements Attendance sheet is going around – be sure you sign it! First part of.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 End of Week Five! Questions? First part of essay assignment (choosing a topic) is posted.
Week 3 Recap CSE 115 – Fall Java Source Code File Made up of: Package declaration Class definition.
UML class diagrams (1) UML = Unified Modeling Language We use only class diagrams, not other UML diagrams Purpose: –keep OO concepts separate from implementation.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
Inheritance using Java
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
1 Chapter 3 and 6– Classes, Objects and Methods Object-Oriented Programming Concepts – Read it from Java TutorialJava Tutorial Definition: A class is a.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ECE122 Feb. 22, Any question on Vehicle sample code?
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Objects as a programming concept
Haidong Xue Summer 2011, at GSU
Objects as a programming concept
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
PHP Classes and Objects
Using local variable without initialization is an error.
Class Inheritance (Cont.)
Classes & Objects: Examples
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Which best describes the relationship between classes and objects?
Classes and Objects CGS3416 Spring 2019.
The Object Paradigm Classes – Templates for creating objects
Chapter 4 Test Review First day
Presentation transcript:

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 Java code, involves 3 changes to whole class: –Declaration of instance variable of part class/type –Instantiation of part class in whole class constructor –Assignment of new part instance to instance variable

2 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }

3 Important points about composition Whole has responsibility for creating its parts (which is why instantiation of parts happens in constructor of whole). Whole can communicate with parts. This is why an instance variable is declared: to establish a name for the newly created object.

4 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Class definition is shown in green:

5 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instance variable name is shown in green:

6 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instance variable declaration is shown in green:

7 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Access control modifiers are shown in green: Note that access control modifier of _tail is private, not public.

8 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Constructor definition is shown in green:

9 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Header of constructor definition is shown in green:

10 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Access control modifier in header of constructor definition is shown in green:

11 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Name of constructor in header of constructor definition is shown in green:

12 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Parameter list in header of constructor definition is shown in green:

13 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instantiation of class Tail is shown in green:

14 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } ‘new’ operator in instantiation of class Tail is shown in green:

15 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Use of constructor in instantiation of Tail class is shown in green:

16 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Argument list in instantiation of class Tail is shown in green:

17 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Assignment of new Tail instance to instance variable is shown in green: