What _is_ an interface? (OOA&D, p. 224) An interface is... - the connection between two objects. - the place where two different objects meet. - the methods that classes promise to implement. An interface in Java: interface Athlete { String getSport(); void play(); } class BasketballPlayer implements Athlete { String getSport() { return "basketball"; } void play() { /* Do stuff */ ... } } ... So what _does_ it mean to code to the interface rather than the implementation? - Forces you to be honest. Each class must implement the interface. Forces implementers to be interchangable. - Exploits polymorphism -- different classes can have different code. - *USERS* of the classes can to refer to the interface, rather than specific classes. - the interface is a type. class Team { ... void play() { ... Athlete player = ... player.play(); ... } ... } ... What's the difference between inheritance and interfaces? [Andy] Superclass:Subclass::Interface:Implementor An interface has no code. An implementor has to implement every method of the interface. Superclasses have code. A subclass can inherit some methods without reimplementing them, and can override some methods also. Abstract classes aren't complete---they have some abstract methods, which have no code. They can't be instantiated, only subclassed. Also have virtual methods (in C) which are not required to be implemented, but if you try to call them you get an exception. An interface in Python: class Athlete(object): # a superclass that looks like an interface. def __init__(self): raise Exception, "This class is abstract." def getSport(self): "Returns a string" raise Exception, "Not implemented." def play(self): pass "Returns nothing." class BasketballPlayer(Athlete): def __init__(self, position): self.position = position def getSport(self): return "basketball" def play(self): ... Don't find out until runtime if a method isn't implemented.