Interfaces - PowerPoint PPT Presentation

About This Presentation
Title:

Interfaces

Description:

... head including the chin, mouth, nose, cheeks, eyes, and usually the forehead. ... The interface is a list of methods and/or constants. ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 21
Provided by: isci
Category:

less

Transcript and Presenter's Notes

Title: Interfaces


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
    Is 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 lt o
  • 0 when it is o
  • positive value when if it is gt 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) lt 0)
  • return b
  • else
  • return a

9
Containers
  • public interface Playable
  • void play()
  • class Song implements Playable ...
  • void play(Playable a)

10
Containers
  • public interface Playable
  • void play()
  • class Song implements Playable ...
  • void play(Playable a)
  • if (a null) return
  • for (int i 0 i lt a.length i)
  • ai.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.
Write a Comment
User Comments (0)
About PowerShow.com