1 / 20

Section 6

Section 6. Interfaces Upcasting Downcasting. Interfaces. inter (Lat.) - between. face ( Eng. ) - the front part of the human head including the chin, mouth, nose, cheeks, eyes, and usually the forehead. interface. Interface. The interface is a list of methods and/or constants.

kittinger
Download Presentation

Section 6

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. Section 6 • Interfaces • Upcasting • Downcasting

  2. Interfaces • inter (Lat.) - between. • face (Eng.) - the front part of the human head including the chin, mouth, nose, cheeks, eyes, and usually the forehead. interface

  3. Interface • The interface is a list of methods and/or constants. • Class implements the interface I, if it has all I’s methods. public interface A { ... } class B implements A {...}

  4. Interfaces: Example public interface Comparable { public int compareTo(Object o); } The implementation should return negative value when the object is “<” o 0 when it is “=“ o positive value when if it is “>” o

  5. Interfaces: Comparable • class Student implements Comparable { public int IQ; ... public int compareTo(Object o) { return IQ - ((Student)o).IQ; } }

  6. Fancier way • class Student implements Comparable { public int IQ; ... public int compareTo(Object o) { return (new Integer(IQ)).compareTo( new Integer(((Student)o).IQ)); } }

  7. Usages: Object max(Comparable a, Comparable b) { }

  8. Usages: Object max(Comparable a, Comparable b) { if (a.compareTo(b) < 0) return b; else return a; }

  9. Containers public interface Playable { void play(); } class Song implements Playable {...} voidplay(Playable []a) { }

  10. Containers public interface Playable { void play(); } class Song implements Playable {...} voidplay(Playable []a) { if (a == null) return; for (int i = 0; i < a.length; i++) a[i].play(); }

  11. Moral • We can write generic functions, which operate on containers of objects implementing interfaces, using interface methods.

  12. Interfaces: Extension • As one class may extend other, one interface may also extend other. interface BritneySong extends Playable { void showBritney(); } • The extending interface inherits all fields/methods of the base interface.

  13. Interfaces: Upcasting Take int f(Comparable c). What arguments can f take? Answer: Objects of any class implementing Comparable, or any interface extending Comparable.

  14. Upcasting • Rationale: This is, in a way, looking at only the part of the object. The fact that the class implements interface assures us that methods we call will be present. • void f( ) - f needs only the part of the object class A implements Comparable int compareTo(...)

  15. Upcasting • Function having an interface as an argument can accept any object implementing this interface.

  16. Upcasting • How it works? void f(Comparable c) { ... c.compareTo(...) } • During the execution of f, the method compareTo is being looked for in the actual object passed to the method.

  17. Downcasting • Object max(Comparable c1, Comparable c2) The code using this function could look like: Integer i = max(new Integer(5), new Integer(23));

  18. Downcasting WRONG! • We need to downcast Object explicitly to Integer. Integer i = (Integer)(max(new Integer(5), new Integer(23))); • Note that: Float f = (Float)(max(new Integer(5), new Integer(23))); would cause a runtime error (exception).

  19. Downcasting • So: • max looks at only the parts of its arguments which are the part of the interface • However, the original objects are still there and we can get access to them by downcasting.

  20. Summary • Upcasting: • Automatic (implicite) • Function with interfaces as arguments. • Arrays of interfaces. • Runtime errors impossible. • Downcasting: • Have to downcast explicite. • Can cause runtime error.

More Related