Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1.

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu 1

2

3 Announcements Exam 2 – 2 weeks away –covers material from exam 1 up to & including 10/21 –review on Monday 10/24 –exam on Wednesday 10/26

4 Agenda null this interfaces

5 ‘null’ ‘null’ denotes the null reference, a reference which does not refer to any object. We can use ‘null’ to solve the two dogs, one collar problem (see code on next slide):

6 removeCollar rather than getCollar public class Dog { private Collar _collar; public Dog(Collar collar) { this._collar = collar; } public void setCollar(Collar collar) { this._collar = collar; } public Collar removeCollar() { Collar temp = this._collar; this._collar = null; return temp; }

7 Can also use in constructor public class Dog { private Collar _collar; public Dog() { _collar = null; }. } Now a Dog can be created without a Collar

8 4925 4850 Consider this code (assume association via constructor) Dog fido = new Dog(new Collar()); Dog rover = new Dog(new Collar()); fido – 3500 rover – 4000 fido’s _collar – 3600 rover’s _collar – 4100 two collars are at 4850 and 4925 3500 4000 fido rover 3500 3600 4000 4100 _collar 4850 4925

9 Dog fido = new Dog(new Collar()); Dog rover = new Dog(new Collar()); fido.setCollar(rover.removeCollar()); public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } Consider this code (assume association via constructor) temp 4925 4850 3500 4000 fido rover 3500 3600 4000 4100 _collar 4850 4925

10 Dog fido = new Dog(new Collar()); Dog rover = new Dog(new Collar()); fido.setCollar(rover.removeCollar()); public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } Consider this code (assume association via constructor) What happens here? Which _collar are we referring to here? temp 4925 4850 3500 4000 fido rover 3500 3600 4000 4100 _collar 4850 4925

11 fido.setCollar(rover.removeCollar()); public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } this the object on which a method is invoked this 4000 4925 temp 4925 4850 3500 4000 fido rover 3500 3600 4000 4100 _collar 4850 4925 What happens here? Which _collar are we referring to here?

12 fido.setCollar(rover.removeCollar()); public Collar removeCollar() { Collar temp = this._collar; this._collar = null; return temp; } this implicitly in code this 4000 4925 temp 4925 4850 3500 4000 fido rover 3500 3600 4000 4100 _collar 4850 4925 What happens here? Which _collar are we referring to here?

13 rover.setCollar(fido.removeCollar()); public Collar removeCollar() { Collar temp = this._collar; this._collar = null; return temp; } this 3500 4850 temp 4925 4850 3500 4000 fido rover 3500 3600 4000 4100 _collar 4850 4925 What happens here? Which _collar are we referring to here?


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."

Similar presentations


Ads by Google