Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces.

Similar presentations


Presentation on theme: "Interfaces."— 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 Iterator iterator() 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 Iterator iterator() 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 Object of 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: Iterator it = 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
interface Speaker { void speak(); } class Dog implements Speaker { public void speak() { System.out.println (“Woof”); class Cat implements Speaker { System.out.println (“Meow”); class Cow implements Speaker { System.out.println (“Moo”); 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(); }

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 public interface ConversionFactors
{ double MM_PER_INCH = 25.4; double GRAMS_PER_OUNCE = ; double GRAMS_PER_POUND = ; double WATTS_PER_HP = 745.7; double HP_PER_WATT = 1 / WATTS_PER_HP; } // don't have to say public, static, or final public interface 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 public interface ConversionFactors
{ double MM_PER_INCH = 25.4; double GRAMS_PER_OUNCE = ; double GRAMS_PER_POUND = ; double WATTS_PER_HP = 745.7; double HP_PER_WATT = 1 / WATTS_PER_HP; } public class MyClass implements ConversionFactors private double poundsWeight; public double getMetricWeight() return poundsWeight * GRAMS_PER_POUND; ...

13 public interface Conversions
{ double inchToMM(double inches); double ounceToGram(double ounces); double poundToGram(double pounds); double HPToWatt(double hp); double wattToHP(double watts); } public class MyClass implements Conversions, ConversionFactors public double 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();


Download ppt "Interfaces."

Similar presentations


Ads by Google