120 likes | 200 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 week 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 week away • covers material from exam 1 up to & including 10/15 • review on Monday 10/18 • exam on Wednesday 10/20
Agenda • interfaces and realization • type hierarchy
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(); }
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; } }
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; } }
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
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!