Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS257 Computer Science I Kevin Sahr, PhD Lecture 11: Interfaces 1 Multiple Inheritance ✴ Some programming languages allow multiple inheritance • a class can inherit from multiple parent classes ✴ For example, say we had a Student class and an Employee class • we might want to create a StudentWorker class to represent students on work study • StudentWorker is-a Student and StudentWorker is-a Employee • multiple inheritance would allow us to inherit from both classes 1- 2 1- 3 Single Inheritance ✴ But, multiple inheritance adds subtle complexities to a language • for example, if both Student and Employee have an instance variable name, which one does StudentWorker use? ✴ The designers of Java decided not to include multiple inheritance • in Java every class can derive from only one parent class • this is called single inheritance ✴ However, some of the functionality of multiple inheritance is provided in Java by interfaces Interfaces ✴ A Java interface is a collection of abstract methods and/or constants ✴ An abstract method is a method header without a method body • An abstract method can be declared in any class using the modifier abstract, but because all methods in an interface are abstract, usually it is left off ✴ An interface is used to establish a set of methods that a class will implement 1- 4 Interfaces interface is a reserved word None of the methods in an interface are given a method body public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } A semicolon immediately follows each method header 1- 5 1- 6 1- 7 1- 8 Interfaces ✴ An interface cannot be instantiated • that is, we can’t create an object with an interface as it’s “class” ✴ A class formally implements an interface by: • stating so in the class header • providing implementations for each method in the interface ✴ If a class asserts that it implements an interface, it must define all methods in the interface Interfaces public class CanDo implements Doable { public void doThis () implements is a { reserved word // whatever } public void doThat () { // something else } Each method listed in Doable is given a definition // etc. for other methods in Doable } Interfaces ✴ As with any class, a class that implements an interface can implement other methods as well ✴ In addition to (or instead of) abstract methods, an interface can contain constants ✴ When a class implements an interface, it gains access to all its constants Interfaces ✴ A class can implement multiple interfaces ✴ All interfaces are listed in the implements clause, separated by commas ✴ The class must implement all methods in all interfaces listed in the header class ManyThings implements Interface1, Interface2 { // define all methods of both interfaces } 1- 9 1- 10 1- 11 Interfaces ✴ The Java standard class library contains many helpful interfaces ✴ The Comparable interface contains one abstract method called compareTo, which is used to compare two objects • it has the method header: public int compareTo (Object obj) ✴ We previously discussed the compareTo method of the String class • The String class implements Comparable, giving us the ability to put strings in lexicographic order The Comparable Interface ✴ Any class can implement Comparable to provide a mechanism for comparing objects of that type if (obj1.compareTo(obj2) < 0) System.out.println ("obj1 is less than obj2"); ✴ When implementing a Comparable interface your compareTo method should follow the convention of returning: • negative value if this is “less than” the argument obj • 0 if they are equal, and • positive value if this is “greater than” the argument obj The Comparable Interface ✴ It's up to the programmer to determine what makes one object less than another ✴ For example, you may define the compareTo method of an Employee class to order employees by name (alphabetically) or by employee number ✴ The implementation of the method can be as straightforward or as complex as needed for the situation ✴ Note that the argument to compareTo is an Object • you will usually need to cast this object to the appropriate class in order to invoke methods on it ✴ See CD.java and CompareCD.java 1- 12 Why Use Interfaces? ✴ Note that you could write a class that implements certain methods (such as compareTo) without formally implementing the interface (Comparable) ✴ However, formally implementing the interface provides a number of benefits • users of your class will already have some idea about how your compareTo should operate, based on their experience with other Comparable’s • enables polymorphism (discussed in the next lecture) 1- 13 1- 14 Lecture 11 Vocabulary multiple inheritance single inheritance interface abstract method Comparable interface