1 / 15

Interfaces

Interfaces. Interfaces. An interface is a Java structure similar to a class an interface defines methods with signatures only (no body) public interface Plumber { public void clearDrain() ; public double computeBill() ; } An interface defines the essence of something.

huggard
Télécharger la présentation

Interfaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interfaces

  2. Interfaces • An interface is a Java structure similar to a class • an interface defines methods with signatures only (no body) public interface Plumber { public void clearDrain(); public double computeBill(); } • An interface defines the essence of something

  3. Interfaces (continued) • Other classes can implement an interface public class Handyman extends Carpenter implements Plumber { • implementation is a contract, indicating that the class will include the methods specified by the interface (real methods, with bodies) • this creates the same kind of “is a” relationship as with superclasses: • a Handyman is a Handyman • a Handyman is a Carpenter • a Handyman is a Plumber

  4. Interfaces (continued) • Interfaces are Java’s approach to multiple inheritance • when extending a superclass, a class inherits the functionality (methods) of the superclass • when implementing an interface, a class agrees to provide the functionality (methods) of the interface • a class may only extend one superclass, but may implement many interfaces public class Handyman extends Carpenter implements Plumber, Electrician, Painter {…}

  5. Iterator Interface • Class ArrayList has the method: public Iteratoriterator()Returns an iterator over the elements in this list in proper sequence. • Iterator is an interface: public interface Iterator { public boolean hasNext(); // true if another object exists public Object next(); // returns next object public void remove(); // removes last object returned } • What does it mean when the return type is an interface?

  6. Iterator Interface • Class ArrayList has the method: public Iteratoriterator()Returns an iterator over the elements in this list in proper sequence. • Iterator is an interface: public interface Iterator { public boolean hasNext(); // true if another object exists public Object next(); // returns next object public void remove(); // removes last object returned } • What does it mean when the return type is an interface? It means that the method returns an Objectof a class that implements that interface. (We don’t know, or care, what the class name is.). We can interact with the object using the interface methods.

  7. Iterator Interface ArrayList list = new ArrayList(); list.add (new Student (“Wemba”, “CSS”, 3.7, 75)); … Iterator it = list.iterator(); while (it.hasNext()) { Student s = (Student)it.next(); System.out.println (s.getName() + “ “ + s.getGPA()); }

  8. Interfaces as Data Types • an interface can be used as a data type when declaring variables, just as a class type can: Iterator it; • however, objects of an interface cannot be instantiated: Iteratorit = new Iterator(); // incorrect! • objects of a class which implements the interface can be assigned to such a variable: it = list.iterator(); // returned object implements Iterator

  9. Polymorphism with Interfaces Speaker[] critters = new Speaker[3]; int k = 0; critters[k++] = new Cat(); critters[k++] = new Dog(); critters[k++] = new Cow(); for (int j=0; j<critters.length; j++) { critters[j].speak(); } interface Speaker { void speak(); } class Dog implements Speaker { public void speak() { System.out.println (“Woof”); } } class Cat implements Speaker { public void speak() { System.out.println (“Meow”); } } class Cow implements Speaker { public void speak() { System.out.println (“Moo”); } }

  10. More About Interfaces • interfaces may contain: • methods • public & abstract by default • have no body (end with semi-colon) • constants • public, static, final by default

  11. publicinterface ConversionFactors { double MM_PER_INCH = 25.4; double GRAMS_PER_OUNCE = 28.349523125; double GRAMS_PER_POUND = 453.5924; double WATTS_PER_HP = 745.7; double HP_PER_WATT = 1 / WATTS_PER_HP; } // don't have to say public, static, or final publicinterface Conversions { double inchToMM(double inches); double ounceToGram(double ounces); double poundToGram(double pounds); double HPToWatt(double hp); double wattToHP(double watts); } // don't have to say public or abstract

  12. publicinterface ConversionFactors { double MM_PER_INCH = 25.4; double GRAMS_PER_OUNCE = 28.349523125; double GRAMS_PER_POUND = 453.5924; double WATTS_PER_HP = 745.7; double HP_PER_WATT = 1 / WATTS_PER_HP; } publicclass MyClass implements ConversionFactors { private double poundsWeight; publicdouble getMetricWeight() { return poundsWeight * GRAMS_PER_POUND; } ... }

  13. publicinterface Conversions { double inchToMM(double inches); double ounceToGram(double ounces); double poundToGram(double pounds); double HPToWatt(double hp); double wattToHP(double watts); } publicclass MyClass implements Conversions, ConversionFactors { publicdouble inchToMM(double inches) { return inches * MM_PER_INCH; } ... // need to implement all methods, // or make class abstract }

  14. interface Alpha { void b(); void c(); } class Beta implements Alpha { void b() { System.out.println(“b() called”); } void c() { System.out.println(“c() called”); } } public class InterfaceExample { public static void main(String[] args) { Alpha x = new Alpha(); Alpha y = new Beta(); y.b(); y.c(); } }

  15. interface Alpha { void b(); void c(); } class Beta implements Alpha { void b() { System.out.println(“b() called”); } void c() { System.out.println(“c() called”); } } public class InterfaceExample { public static void main(String[] args) { Alpha x = new Alpha(); // no way Alpha y = new Beta(); // OK y.b(); y.c(); } }

More Related