200 likes | 217 Views
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.
E N D
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. • Class implements the interface I, if it has all I’s methods. public interface A { ... } class B implements A {...}
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
Interfaces: Comparable • class Student implements Comparable { public int IQ; ... public int compareTo(Object o) { return IQ - ((Student)o).IQ; } }
Fancier way • class Student implements Comparable { public int IQ; ... public int compareTo(Object o) { return (new Integer(IQ)).compareTo( new Integer(((Student)o).IQ)); } }
Usages: Object max(Comparable a, Comparable b) { }
Usages: Object max(Comparable a, Comparable b) { if (a.compareTo(b) < 0) return b; else return a; }
Containers public interface Playable { void play(); } class Song implements Playable {...} voidplay(Playable []a) { }
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(); }
Moral • We can write generic functions, which operate on containers of objects implementing interfaces, using interface methods.
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.
Interfaces: Upcasting Take int f(Comparable c). What arguments can f take? Answer: Objects of any class implementing Comparable, or any interface extending Comparable.
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(...)
Upcasting • Function having an interface as an argument can accept any object implementing this interface.
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.
Downcasting • Object max(Comparable c1, Comparable c2) The code using this function could look like: Integer i = max(new Integer(5), new Integer(23));
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).
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.
Summary • Upcasting: • Automatic (implicite) • Function with interfaces as arguments. • Arrays of interfaces. • Runtime errors impossible. • Downcasting: • Have to downcast explicite. • Can cause runtime error.