150 likes | 169 Views
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.
E N D
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
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
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 {…}
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?
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.
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()); }
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
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”); } }
More About Interfaces • interfaces may contain: • methods • public & abstract by default • have no body (end with semi-colon) • constants • public, static, final by default
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
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; } ... }
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 }
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(); } }
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(); } }