160 likes | 278 Views
CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu. Phones off Signs out. Announcements. Exam 2 – 1.5 weeks away covers material from exam 1 up to & including 10/15 review on Monday 10/18 exam on Wednesday 10/20. Agenda.
E N D
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu
Phones off Signs out
Announcements • Exam 2 – 1.5 weeks away • covers material from exam 1 up to & including 10/15 • review on Monday 10/18 • exam on Wednesday 10/20
Agenda • association relationship • null (review) • this • interfaces and realization
‘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):
removeCollar rather than getCollar public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void setCollar(Collarcollar) { _collar = collar; } public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } }
Can also use in constructor public class Dog { private Collar _collar; public Dog() { _collar = null; } . . . } Now a Dog can be created without a Collar
Consider this code(assume association via constructor) fido rover 3500 4000 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 • 4850 _collar • _collar 3500 3600 4000 4100 • 4925 4850 4925
Consider this code(assume association via constructor) fido rover 3500 4000 temp 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; } • 4850 _collar • _collar 3500 3600 4000 4100 • 4925 4850 4925
Consider this code(assume association via constructor) fido rover 3500 4000 temp 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; } • 4850 _collar • _collar 3500 3600 4000 4100 What happens here? Which _collar are we referring to here? • 4925 4850 4925
thisthe object on which a method is invoked fido rover this 3500 4000 temp fido.setCollar(rover.removeCollar()); public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } • 4000 • 4925 • 4850 _collar • _collar 3500 3600 4000 4100 What happens here? Which _collar are we referring to here? • 4925 4850 4925
thisimplicitly in code fido rover this 3500 4000 temp fido.setCollar(rover.removeCollar()); public Collar removeCollar() { Collar temp =this._collar; this._collar= null; return temp; } • 4000 • 4925 • 4850 _collar • _collar 3500 3600 4000 4100 What happens here? Which _collar are we referring to here? • 4925 4850 4925
this fido rover this 3500 4000 temp rover.setCollar(fido.removeCollar()); public Collar removeCollar() { Collar temp =this._collar; this._collar= null; return temp; } • 3500 • 4850 • 4850 _collar • _collar 3500 3600 4000 4100 What happens here? Which _collar are we referring to here? • 4925 4850 4925
Interfaces and Realization • form of interface definition • function of an interface • realization (“implements”) relationship
Form of an interface • header + body • header • access control modifier • keyword ‘interface’ • name (generally an adjective, following class name conventions, but prefixed with an upper-case ‘I’) • body • method specifications (method headers followed by ‘;’, also called method declarations, as opposed to method definition)
Example public interface ICollarable { public void addCollar(Collar collar); public Collar removeCollar(); }