Sept. 29th Interfaces We used the ther term interface to mean the public methods through which the client cfode can interact with an object. That definitino ius pretty general. Now we are going to learn the formal meaning of interface in a particular language construct in Java. Note that students studying for the A exam are expected to have a reading knowledge of interfaces while students studying for the AB exam are expected to be able to define their own inteffaces. A Java interface is a collection of constants and abstract methods. An abstract method is a method that does not have an implementation. That is, there is no body of code defined for an abstract method. The header of the method, including its parameter list, is simply followed by a semicolon. AN interface cannot be instantiated. Listing 5.6 shows an interface called Complexity. It contains two abstract methods: setComplexity and getComplexity. An abstract method can be preceded by the reserved word abstract, though in interfaces it usually is not. All methods in interfaces have public visibility by default. Listing 5.7: ************************** public interface Complexity { public void setComplexity(int complexity); public int getComplexity(); } ********************************* A class implements an interface by providing method implementations for each of the abstract methods defined in the interface. A class that implements an interface uses the reserved word implements followed by the inferface name (e.g., Complexity) in the class header. When a class implements an interfac,e it must give a defintiion for all methods in the interface. The compiler will produce errors if any of these methods in the interface are not given a definition in the class. The Question class, shown in Listing 5.8, implements the Complexity interface from Listing 5.7. Both the setComplexity and getComplexity methods are implemented. they must be declared with the same signatures as the abstracct methods (setComplexity and getComplexity) in the interface. In the Question class, the methods are defined simply to set or return a numeric value representing the complexity levle of thee question that the object represents. note that the Question class also implements methods that are not part of hte Compolexity interfac,e methods called getQuestion, getAnswer, answerCorrect, and toString, which ahve nothing to do with the interface. The interface guarantees that the class implements certain methods, but it can also have others. In fact, a class that implements an interface usually has other methods, too. Listing 5.9 shows ap rogram called MiniQuiz, which uses some Question objects. Listing 5.8 ************************************* public class Question implements Complexity { private String question, answer; private int complexityLevel; public Question(string query, String result) { question = query; answer=result; complexityLevel = 1; } public void setComplexity(int level) { complexityLevel = leve; } public int getComplexity() { return complexityLevel; } public String getQuestion() { return question; } public String getAnswer() { return answer; } public boolea answerCorrect(String candidateAnswer) { return answer.equals(candidateAnswer); } public String toString() { return question + "\n" + answer; } } ******************************* The listing 5.9 is not present due to a lack of relevance. Several classes can implement the same interface, igiving different definitions for the methods. For example, we could implement a class called task that also implements theComplexity interface. In it we could choose to manage the complexity of a task in a different way (though it would still have to implement all the methods of the interface). A class can implement more than one interface. In these casses, the class must implement all methods in all interfaces listed. To show that a class implements multip;le interace,s we list them in the implements clause, separated by commas: For Example: class ManyThings implements interface1, interface2, interfaceC An interface can also contain constants, defined using the final modifier. When a class implements an interface, it can use all of the constants defined in it. This lets everal classes share a set of constants. The interface construct formally tells us how we can interfact with a class. It is also the basis for a powerful programming technique claled polymorphism, which we will discuss in Chapter 7. ------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- October 4th, 02005 Interfaces come from abstract classes. Abstract classes are.... Whenever you start a program, .. Interfaces are very important. A java interface is a collection of abstract methods and constants. Finding the effeciency of an algorithm may be challenging, though not interfaces. Why do we use interfaces? An abstract method is something that is not defined, it is, however, declared. An abstract method is a method header without a method body. All that yuou do is list the parameters, the name of the method, etc. There is no internal code. An abstract method can be declared using the modifier abstract, but because all methods in an interface are bastract, usually it is left off Methods in an interface are automatically abstract An iterface is used to establish, as a formal contract, a set of methods that a class will implement An example. A class Cars. Every type of car will have to implement some basic methods. These basic methods are the interfaces. When you create an interface, you put these method definitions in it. How do we implement these methods regardless of which type of Car we have? public interface Doable { public void doThis(); public int doThat(); public void doThis2(double value, char ch); public boolean dotheOther(int num); } Any class that implements the interface, it has to declare those methods. "interface" is a reserved word. It's a keyword. Special word. None of the methods in a n interface are given a definition (body). A semicolon immediately follows each method header. An interface cannot be instantiated. methods in an interface have public visibility by default Methods in an interface have public visibility by default A class formally implements an interface by stating so in the class header, and by providing implementations for each abstract method in the interface. If a class asserts that it implements an interfac,e it must define all methods in the interface public class CanDo implements Doable { public void doThis() { /* whatever */ } public void doThat() { /* whatever */ } // etc. } "implements" is the reserved word public class ClassName implements Interface1, Interface2, A class that implements an interface can implement other methods as well.