// Inheritance basics /* An interface defines the type of objects. That is, it specifies to the rest of the program what (public) functions your objects offer. A class "implements" an interface by providing code for all the functions specified in the interface. In older programming languages (C/C++) the interface concept was manifested as "header files". A superclass contains common code shared by subclasses. A subclass "extends" a superclass. A class can extend only one superclass, but can implement multiple interfaces. An interface can extend another interface. Every class has an implicit interface, which consists of all the public methods of the class. Unless an "extends" specification is provided, every class extends the superclass "Object" by default. */ interface team { void win(); void lose(); int gamesplayed(); } class teambase implements team // code common to all teams { protected int wins, losses; // protected means visible to subclasses public teambase(int w, int l) { wins=w; losses=l; } public void win() { wins++; } public void lose() { losses++; } public int gamesplayed() { return wins + losses; } } class footballteam extends teambase implements team { private String quarterback; public footballteam(String q) { super(0,0); // calls superclass constructor quarterback = q; } } class basketballteam extends teambase implements team { public String pointguard; public basketballteam() { super(0,0); } } // new interface for all teams that can tie - it includes all declarations // of the team interface: interface tieteam extends team { void tie(); } class hockeyteam extends teambase implements tieteam { private String goalie; private int ties; // hockey teams can tie public void tie() { ties++; } public hockeyteam(String g) { super(0,0); ties = 0; goalie = g; } // an "overriding" method - replaces method inherited from superclass: public int gamesplayed() { return wins + losses + ties; } } // hockeyteam public class teams { public static void main(String[] args) { team jets = new footballteam("whoever"); team knicks = new basketballteam(); tieteam islanders = new hockeyteam("whocares"); jets.win(); jets.lose(); knicks.lose(); knicks.lose(); islanders.win(); islanders.lose(); islanders.tie(); team[] NY = new team[3]; NY[0]=jets; NY[1]=knicks; NY[2]=islanders; // What's interesting about the above array is that // it contains objects of different types, because it's // declared to be an array of "teams" for(int i=0;i