Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 alphonce@buffalo.edu

2 Announcements Install session tonight: –Baldy 21 @ 5:00 PM Exam 2 on Wednesday 10/21 –covers material from last exam up to and including Friday 10/16 Review on Monday 10/19

3 Recall the Dog class public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void setCollar(Collar collar) { _collar = collar; } public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; }

4 Consider this code (assume association via constructor) Dog fido = new Dog(new Collar()); Dog rover = new Dog(new Collar()); 1025 825 fido rover 800 825 1000 1025 800 1000 _collar

5 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) 1025 825 fido rover temp 800 825 1000 1025 800 1000 _collar

6 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) 1025 825 fido rover temp 800 825 1000 1025 800 1000 _collar What happens here? Which _collar are we referring to here?

7 I left you with this puzzle to think about…

8 fido.setCollar(rover.removeCollar()); public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } this 1025 825 800 fido rover this temp 800 825 1000 1025 800 1000 _collar What happens here? Which _collar are we referring to here?

9 rover.setCollar(fido.removeCollar()); public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } this 1025 825 1025 1000 fido rover this temp 800 825 1000 1025 800 1000 _collar What happens here? Which _collar are we referring to here?

10 Interfaces and Realization form of interface definition function of an interface realization (“implements”) relationship

11 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)

12 Example public interface ICollarable { public void addCollar(Collar collar); public Collar removeCollar(); }

13 Example public class Dog implements ICollarable { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void setCollar(Collar collar) { _collar = collar; } public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; }

14 Example public class Dog implements ICollarable { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void addCollar(Collar collar) { _collar = collar; } public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; }

15 Example ICollarable x; x = new ICollarable(); NO! Cannot instantiate. x = new Dog(); OK if Dog implements ICollarable x = new Cat(); OK if Cat implements ICollarable

16 Calling methods ICollarable x; x = new ICollarable(); NO! Cannot instantiate. x = new Dog(); OK if Dog implements ICollarable x = new Cat(); OK if Cat implements ICollarable x.addCollar(new Collar()); OK x.removeCollar(); OK x.walk(); NO! Not all ICollarable objects define this method!


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"

Similar presentations


Ads by Google