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) { ... } }
interface
suBloggingFeed
interface.
TwitterFeed
class implements the uBloggingFeed
interface by
implementing the instance methods defined in the interface.
TwitterFeed
can implement other methods too.
new
calls).
implements
modifier to indicate which interfaces
it provides.
class X implements uBloggingFeed, IPod, { ... }
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>();
TwitterFeed
connects to Twitter.
IdenticaFeed
connects to Identi.ca.
TwitterFeed
and IdenticaFeed
.
poll()
method for new messages.
poll()
look like?
TwitterFeed
and IdenticaFeed
are different types.
public static void poll(TwitterFeed) f) { final String msgs [] = f.read(); if (msgs.length > 0) { ... } } public static void poll(IdenticaFeed f) { final String msgs [] = f.read(); if (msgs.length > 0) { ... } }
TwitterFeed
and IdenticaFeed
are different types, and
can’t share poll()
.
TwitterFeed
and IdenticaFeed
share poll()
?
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 }
class Client { public static void poll(uBloggingFeed f) { final String msgs [] = f.read(); if (msgs.length > 0) { ... } } // and so on. }
poll()
can accept both TwitterFeed
and IdenticaFeed
instances.
TwitterFeed
and IdenticaFeed
“behave like” an
uBloggingFeed
.
uBloggingFeed p1,p2;
new
.
int i = 3, j = 4; Integer x = new Integer(i + 3*j - 1); System.out.println(x.toString());
i
, j
and the expression i + 3*j - 1
have
int
static type.
x
and the expression new Integer(...)
have static
type Integer
.
x.toString()
has static type String
.
new Integer(...)
has dynamic type
Integer
.
Integer
for example.
int
, boolean
, char
, and so on.
int
and Integer
?Hashtable
, Vector
, Stack
and so on.
for (int i = 0; i < n; i++) { ... }
Given an expression E producing an object O, O’s dynamic type is a subtype of E’s static type.
Object x = new Integer(13);
Integer x = (Integer) y;
ClassCastException
results if it doesn’t.
void red() { white(new String("x")); } void white(Object y) { int z = ((Integer) y).intValue();
uBloggingFeed p1 = new IdenticaFeed(); uBloggingFeed p2 = new TwitterFeed();
p1
and p2
have uBloggingFeed
static type.
new
expressions have types that are subtypes of uBloggingFeed
.
uBloggingFeed
interface.
interface uBloggingFeed { void register(String user, String password); String [] read(); void update(String msg); }
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. }
class Client { public static void poll(uBloggingFeed f) { final String msgs [] = f.read(); if (msgs.length > 0) { ... } } // and so on. }
read
method is invoked?
p
’s dynamic type (TwitterFeed
or
IdenticaFeed
).
read()
method.
class Client { public static void poll(uBloggingFeed f) { final String msgs [] = f.read(); if (msgs.length > 0) { ... } } // and so on. }
uBloggingFeed
)
for read()
’s signature.
read()
method.
uBloggingFeed [] feeds = new uBloggingFeed[9]; feeds[0] = new TwitterFeed(); feeds[1] = new IdenticaFeed();
feeds[i]
has uBloggingFeed
static type.
new
expressions have uBloggingFeed
subtypes as static types.
interface Blob { ... } class Spot implements Blob { ... } class Drop implements Blob { ... }
|
|
class Blob { ... } class Spot extends Blob { ... }
instanceof
binary operator tests dynamic types.
if (p instanceof TwitterFeed) { ... }
i instanceof C
is true if i
’s dynamic type is a
subtype of C
.
instanceof
ExampleJaikuFeed
but not IdenticaFeed
implements the
search()
method.
void search(uBloggingFeed[] feeds) { for (uBloggingFeed f : feeds) if (f instanceof JaikuFeed) { JaikuFeed f = (JaikuFeed) feeds[i]; f.search(); } }
void moveAll(uBloggingFeed[] feeds) { for (uBloggingFeed p : feeds) if (p instanceof TwitterFeed) ((TwitterFeed) feeds[i]).move("N"); else ((IdenticaFeed) feeds[i]).move("N"); }
void moveAll(uBloggingFeed[] feeds) { for (uBloggingFeed p : feeds) feeds[i].move("N"); }
uBloggingFeed
defines register()
, read()
, and update()
methods.
JaikuFeed
also defines the search()
method.
JaikuFeed
interface.
uBloggingFeed
interface to create JaikuFeed
.
interface uBloggingFeed { void register(String user, String password); String [] read(); void update(String msg); } interface JaikuFeed extends uBloggingFeed { String [] search(String keywords); }
uBloggingFeed
is a superinterface of JaikuFeed
.
JaikuFeed
is a subinterface of uBloggingFeed
.
JaikuFeed
is a subtype of uBloggingFeed
.
This page last modified on 22 September 2010. |