Data Structures & Algorithms Lecture Notes

22 September 2010 • Interfaces in Practice


Outline

Interface Example

interface uBloggingFeed {
  void register(String user, String password);
  String [] read();
  void update(String msg);
  }

class TwitterFeed
implements uBloggingFeed {
  void register(String user, String password) { ... }
  String [] read() { ... }
  void update(String msg) { ... }
  }

Java interfaces

Comments

Why Interfaces?

Interface Type Examples

Map<String, Command> h1
  = new HashMap<String, Command>();

Map<String, Command> h2
  = new TreeMap<String, Command>();

List<Object> t1 = new ArrayList<Object>();

List<Object> t2 = new LinkedList<Object>();

Code Duplication Example.

Code Duplication Example..

Comments

Interfaces vs Sharing.

interface uBloggingFeed {
  String [] poll();
  // and so on
  }

Class TwitterFeed
implements uBloggingFeed {
  public String [] poll() { ... }
  // and so on
  }

Class IdenticaFeed
implements uBloggingFeed {
  public int poll() { ... }
  // and so on
  }

Interfaces vs Sharing..

class Client {
  public static void poll(uBloggingFeed f) {
   final String msgs [] = f.read();
   if (msgs.length > 0) { ... }
   }
  // and so on.
  }

Subtype Polymorphism

Ahierarchical Types

Code Sharing

Static vs Dynamic Types

Example

int i = 3, j = 4;
Integer x = new Integer(i + 3*j - 1);
System.out.println(x.toString());

References vs Primitive Types

Why Both int and Integer?

Upcasting and Downcasting

Upcasting

Downcasting

Runtime Checking

Interface Upcasts

Why Upcast?

Sharing With Interfaces

interface uBloggingFeed {
  String [] read();
  // and so on
  }

Class TwitterFeed
implements uBloggingFeed {
  public String [] read() { ... };
  // and so on
  }

Class IdenticaFeed
implements uBloggingFeed {
  public String [] read() { ... };
  // and so on
  }

class Client {
  public static void poll(uBloggingFeed f) {
   final String msgs [] = f.read();
   if (msgs.length > 0) { ... }
    }
  // and so on.
  }

Method Dispatch.

class Client {
  public static void poll(uBloggingFeed f) {
   final String msgs [] = f.read();
   if (msgs.length > 0) { ... }
    }
  // and so on.
  }

Method Dispatch..

class Client {
  public static void poll(uBloggingFeed f) {
   final String msgs [] = f.read();
   if (msgs.length > 0) { ... }
    }
  // and so on.
  }

Comments

Polymorphic Data Structures

uBloggingFeed [] feeds = new uBloggingFeed[9];
feeds[0] = new TwitterFeed();
feeds[1] = new IdenticaFeed();

Type Compatibility

Compatability Example

Static and Dynamic Types

Type Example

Checking Dynamic Types

instanceof Example

Avoid Useless Downcasting

Subinterfaces

Subinterface Example

interface uBloggingFeed {
  void register(String user, String password);
  String [] read();
  void update(String msg);
  }

interface JaikuFeed
extends uBloggingFeed {
  String [] search(String keywords);
  }

Subinterface Properties

Summary


This page last modified on 22 September 2010.

Creative
    Commons License