CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann
Michael Eckmann - Skidmore College - CS Spring 2007 Today’s Topics questions, comments? interfaces
abstract methods Instantiable == non-abstract The reason we create an abstract method (with an empty method body) in the superclass is so that this guarantees that all subclasses must provide this method if they are to be instantiable. Notice: –a non-abstract subclass of Pet had to include code for the speak() method –we couldn't instantiate an object of type Pet because it is abstract
Interfaces An interface is a collection of constants and abstract methods. – An abstract method is one that is defined by a header but no body. An abstract method, when defined in an interface, does not provide an implementation. An interface can never be instantiated. What does that mean again? The way that interfaces are used are classes can implement them. If some class implements an interface then that class IS REQUIRED TO provide code for ALL the abstract methods in the interface. The purpose of interfaces are to formally define the ways in which we can interact with a class (that implements the interface).
Interfaces We can create our own interfaces (and we will later). We can also use interfaces in the Java API.
Interfaces There are many interfaces in the Java API available for us to use. One of these is the interface Comparable It contains one abstract method: – int compareTo( object_to_be_compared ) – It returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Negative means and 0 means equal. It is up to a class that implements the Comparable interface to determine what it means for its objects to be or = to each other. That is specified in the implementation of the method.
Interfaces Example use of Comparable int compareTo( object_to_be_compared ) Let's edit our Card class to implement Comparable. Will we have to add a compareTo method?
Interfaces Example use of Comparable int compareTo( object_to_be_compared ) Let's edit our Card class to implement Comparable. Will we have to add a compareTo method? – Yes! If the class implements Comparable it must provide implementation of ALL the abstract methods of the interface. What might our compareTo method do in the Card class?
Interfaces int compareTo( object_to_be_compared ) Let's edit our Card class to implement Comparable. Will we have to add a compareTo method? – Yes! If the class implements Comparable it must provide implementation of ALL the abstract methods of the interface. What might our compareTo method do in the Card class? – It should return a negative # if the “calling” Card object is less than the Card object that is passed in as a parameter. – It should return 0 if the Card objects are equal – Otherwise return a positive number.
Interfaces The Card class has rank and suit as instance variables. Our compareTo method will look like: public int compareTo(Object o) { // how do we refer to the values // associated with the “calling” Card object? // how do we refer to the values // associated with the parameter object? }
Interfaces Let's create another class that implements Comparable. How about a class to store contact information for someone. Let's store a person's first name, last name, and phone number. Let's create an array of Contacts and then sort that array. –We can start from the bubbleSort method which sorts an array of ints, but we'll make it sort an array of Comparables.