Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.

Similar presentations


Presentation on theme: "2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc."— Presentation transcript:

1 2/23- Interfaces Reference: Java 6.5 and 9.3

2 PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc

3 Interfaces are “Java’s substitute for C++’s feature of multiple inheritance…“ “…used when you want to define a certain functionality to be used in several classes, but are not sure exactly how this functionality will be defined by those classes…” Joe Weber and Mike Afergan in Using Java 2 nd Edition

4 Interfaces Provide constants Provide abstract methods Cannot be instantiated

5 Placing classes in an interface outlines common behavior leaves specific implementation to the classes themselves Makes using interfaces instead of classes a better choice when dealing with advanced data handling. Joe Weber and Mike Afergan in Using Java 2 nd Edition

6 Interfaces are underprivileged first cousins of classes define a set of methods and constants to be implemented by another object help define the behavior of an object by declaring a set of characteristics for the object cannot specify any implementation for methods An interface is used to establish, as a formal contract, a set of methods that a class will implement – Lewis and Loftus Joe Weber and Mike Afergan in Using Java 2 nd Edition

7 Classes that implement an interface are responsible for specifying the implementation of the methods in the interface must override every method in the interface must use the same signature for each overridden method may define other methods

8 Java syntax Instead of a class, when you create an interface, you define it as such: public interface Complexity And to implement an interface: public class Question implements Complexity

9 Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } interface is a reserved word None of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

10 Interfaces An interface cannot be instantiated Methods in an interface have public visibility by default A class formally implements an interface by stating so in the class header providing implementations for each abstract method in the interface If a class asserts that it implements an interface, it must define all methods in the interface

11 Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition

12 Interfaces A class that implements an interface can implement other methods as well See Complexity.java (page 294)Complexity.java See Question.java (page 295)Question.java See MiniQuiz.java (page 297)MiniQuiz.java 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

13 UML for interfaces > Complexity MiniQuiz Question In UML, the dashed arrow with the open head shows an implementation relationship. The dashed arrow from MiniQuiz to Question shows a uses relationship.

14 Another Example Product.java Shoe.java Store.java Project

15 Interfaces A class can implement multiple interfaces The interfaces are listed in the implements clause The class must implement all methods in all interfaces listed in the header class ManyThings implements interface1, interface2 { // all methods of both interfaces }

16 Example and notes public interface Product { static final String MAKER = "My Corp"; static final String PHONE = "555-123-4567"; public int getPrice (int id); } file is named Product.java getPrice is an abstract method – doesn’t require the word abstract and we will omit it. getPrice has no body (nor can it have one) When attributes (fields) are present they must be static and are final

17 Shoe.java public class Shoe implements Product { public int getPrice (int id) { if (id == 1) return (5); else return (10); } public String getMaker() { return (MAKER); } }

18 Store.java public class Store { static Shoe hightop; public static void main (String arg[] ) { init(); getInfo (hightop); orderInfo(hightop); } public static void init () { hightop = new Shoe(); } public static void getInfo (Shoe item) { System.out.println (" This Product is main by " + item.MAKER); System.out.println (" It costs $" + item.getPrice(1) + '\n'); } public static void orderInfo (Product item) { System.out.println (" To order from " + item.MAKER + " call " + item.PHONE + "."); System.out.println (" Each item costs $" + item.getPrice(1)); } } // end class Store

19 When you create an interface None of your methods may have a body All of your methods will be public No non-constant variables may be declared Our interfaces will all be public Interface filename must match interface name

20 extending Interfaces can not extend classes Interfaces can only extend other interfaces If you implement an extended interface, you must override both the methods in the new interface and the methods in the old interface

21 Remember these Classes implement interfaces to inherit their properties Methods in classes must have bodies unless they are abstract methods (in which case they are in abstract classes) Syntax of a method declaration in an interface is public return_value nameOfMethod (parameterList) throws ExceptionList ;

22 Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains an abstract method called compareTo, which is used to compare two objects The String class implements Comparable, giving us the ability to put strings in lexicographic order The Iterator interface contains methods that allow the user to move easily through a collection of objects

23 The Comparable Interface The Comparable interface provides a common mechanism for comparing one object to another if (obj1.compareTo(obj2) < 0) System.out.println (“obj1 is less than obj2”); The result is negative is obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2 When a programmer writes a class that implements the Comparable interface, it should follow this intent It's up to the programmer to determine what makes one object less than another

24 The Iterator Interface The Iterator interface provides a means of moving through a collection of objects, one at a time The hasNext method returns a boolean result (true if there are items left to process) The next method returns the next object in the iteration The remove method removes the object most recently returned by the next method

25 Let’s look at some interfaces in javadocs java.sun.com


Download ppt "2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc."

Similar presentations


Ads by Google