Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Association relationship We’ve seen one implementation of “knows a”: public class Dog.

Similar presentations


Presentation on theme: "Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Association relationship We’ve seen one implementation of “knows a”: public class Dog."— Presentation transcript:

1 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Association relationship We’ve seen one implementation of “knows a”: public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } Now we will see a more flexible implementation.

2 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I2 Essence of association We use an association relationship when we want to model that one object can communicate with another object (as in the composition relationship) but –there isn’t any lifetime link between the two objects, or –the objects in the relationship can change over time (think of Clifford’s collar).

3 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I3 Changing the known property We want to be able to set a new value for the property (e.g. give Clifford a new collar). How can we do that? Using a “mutator” or “setter” method.

4 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I4 mutator method public class Dog{ private Collar _collar; public Dog(Collar collar){ _collar = collar; } public void setCollar(Collar collar){ _collar = collar; }

5 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I5 Constructor/mutator similarity Note the similarity between the constructor and the mutator. With respect to the _collar variable, they are doing essentially the same thing: allowing the invoker to set the value of the instance variable. Difference is that constructor sets value when Dog object is created, mutator changes the value at some later point in time.

6 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I6 Retrieving the known property Suppose an object wants to let other objects know the value of one of its properties? How can we do that? We can define an “accessor” or “getter” method.

7 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I7 accessor method public class Dog{ private Collar _collar; public Dog(Collar collar){ _collar = collar; } public void setCollar(Collar collar){ _collar = collar; } public Collar getCollar() { return _collar; }

8 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I8 Example Dog fido = new Dog(new Collar()); Dog rover = new Dog(new Collar()); rover.setCollar(fido.getCollar());

9 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I9 mutator method (revisted) public class Dog{ private Collar _collar; public Dog(Collar collar){ this.setCollar(collar); } public void setCollar(Collar collar){ _collar = collar; }

10 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I10 return statement The return statement is used to send a value back to the caller of the method. In this example the reference to the object named _collar is sent. Notice the difference in the header of the accessor and mutator methods (next slide).

11 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I11 accessor/mutator differences public void setCollar(Collar collar){ _collar = collar; } public Collar getCollar() { return _collar; }

12 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I12 accessor/mutator differences public void setCollar (Collar collar) a.c.m. r.t.s. name parameter list public Collar getCollar () a.c.m. r.t.s. name parameter list a.c.m. = access control modifier r.t.s. = return type specification void = no value is returned by method (note difference with constructors: no r.t.s.)

13 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I13 Parameter list difference public void setCollar (Collar collar) public Collar getCollar () Accessors and mutators can be defined for any of the instance variables declared in a class. A mutator method needs a value to set the instance variable to. The mutator method is parameterized in its behavior. An accessor method always does the same thing: it returns the current value of the instance variable. The accessor method is therefore not parameterized in its behavior.

14 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I14 What about public/private? They are access control modifiers: they control access to members of a class (instance variables and methods are called members). A member which is public can be accessed from outside of the class definition. This is the least restrictive form of access control. A member which is private can only be accessed from inside the class definition. This is the most restrictive form of access control.

15 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I15 Why accessors/mutators? Why use accessors and mutators, rather than just make instance variables public? public grants both read/write access. With accessors/mutators you can be selective in allowing just one or the other (or both). Accessors/mutators are methods, and can do more than simply grant read/write access to instance variables (Bank account example). Accessors/mutators can exist for “virtual” instance variables: –Many graphical objects provide both a getLocation/setLocation pair, as well as a getCenterLocation/setCenterLocation pair. In reality, only one location is stored, the other is calculated. Which is stored? Who cares? The client of the code does not need to know – the methods will do the right thing. The implementation can even change and the methods will still work correctly.


Download ppt "Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Association relationship We’ve seen one implementation of “knows a”: public class Dog."

Similar presentations


Ads by Google