Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— Presentation transcript:

1 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) No necessary lifetime link We will look at two different implementations of “knows a”: –The first we will see today, and is very similar to our implementation of “has a”. –The second, which we will see next time, is a bit more complex but is also more flexible.

2 Fall 2005CSE 115/503 Introduction to Computer Science I2 First implementation In Java code, the first involves 3 changes to the “knowing” class: –Declaration of instance variable of the “known” class/type (because the “knowing” object will want to communicate with the “known” object). –Assignment of existing “known” instance to the instance variable (because the instance variable must refer to an object). –Parameter of “known” class in “knowing” class constructor (because the creator of an instance of the “knowing” class needs to supply an instance of the “known” class).

3 Fall 2005CSE 115/503 Introduction to Computer Science I3 Dog – Collar example in Java public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; }

4 Fall 2005CSE 115/503 Introduction to Computer Science I4 Lifetime Notice in this first implementation that although the “knowing” class (the Dog) does not create an instance of the “known” class (the Collar), there is a lifetime dependency: the Collar object must exist before the Dog object can be created (since a Collar object must be used in the constructor of the Dog object).

5 Fall 2005CSE 115/503 Introduction to Computer Science I5 Lifetime (continued) This occurs in this particular implementation of the relationship, but is not an essential characteristic of the relationship.


Download ppt "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)"

Similar presentations


Ads by Google