CS51 - Fall 2009 - Lecture 12

  • looking ahead at schedule for next few weeks.
  • midterm is 2 weeks from today.
  • sample midterm: Spring 2006
  • review of while loops and classes
  • extended discussion of growing/shrinking ball example (10.5.2)
  • Interfaces
  • public methods are what you need to use a class, private methods are for the person implementing the class.
  • Java interfaces let you note that two different classes are similar in that they each implement a certain set of methods (though they may do so in completely different ways, and each may have additional methods)
  • ShirtsAndPants
  • Uses an interface called Laundry, in the Laundry.java file:
                public Interface Laundry {
    
                    public boolean contains(Location pt);
                    public void setColor(Color c);
    
                    // also move and moveTo 
    
                }
             
  • Note that there's no constructor
  • There's no body to any of the methods
  • Now TShirt and Pants must note that they implement the interface:
                public Class TShirt implements Laundry {
    
                    ...
    
                }
             
  • means that they each contain the methods defined in the Laundry interface
  • Note how item is declared and used in the main LaundrySorter class.
  • private Laundry item;
  • Now can call methods on item that are declared in the Laundry interface.