abstract
keyword identifies features intended for protocol
definition.
interface Candy { public abstract int calorieCount(); Nuts [] containsNuts(); boolean hasFlavonoids(); } interface 3DRectangle { double height(); double width(); double length(); }
interface ChocolateBar extends Candy { ChocolateKind chocolateKind(); // and so on. }
public interface ChocolateBar extends Candy, 3DRectangle { ... }
class ThreeMusketeers implements ChocolateBar int calorieCount() { ... } boolean hasFlavonoids { return true; } // and so on
interface Universals { public static final int tearsCried = 96, theAnswer = 42; }
abstract class Universals { public static final int tearsCried = 96, theAnswer = 42; }
interfaceComparable
int compareTo(Object o); public interfaceDestroyable
void destroy(); boolean isDestroyed(); public interfaceReadable
int read(CharBuffer
cb);
class Card implementsComparable
int compareTo(Object o) { ... } // and so on. class SecurityCertificate implementsDestroyable
,Readable
void destroy() { ... } boolean isDestroyed() { ... } int read(CharBuffer cb) { ... } // and so on.
boolean beats(Comparable
x) { ... } void validate(Destroyable
tag) { ... } Message read(Readable
input) { ... }
SecurityCertificate ticket = new SecurityCertificate() validate(ticket) Message m = read(ticket)
class banana implements peel, soft {...} class apple implements seeds, crisp {...}
interface soup extends stock, aromatics {...} interface salad extends greens, dressing {...}
interface MouseListener void mouseMotion() void mouseButton() // lots more abstract class AbstractMouseListener implements MouseListener public void mouseMotion() { ... } public void mouseButton() { ... } // lots more class TouchPadListener extends AbstractMouseListener public void mouseMotion() { ... }
- Problem solved?
class T // Or maybe T(T t) T copy() cpy = new T() for each v: this.instance-variable cpy.v = v return cpy
copy()
implements a shallow copy.
T deepCopy()
cpy = new T()
for each v: this.instance-variable
cpy.v = v.deepCopy()
return cpy
Blob cpy = ci.deepCopy()
Object.clone()
protectedObject
clone()
implements copying.
$ cat t.java class blob { } class t { public static void main(String
args[]) { blob b = new blob(); blob cpy = (blob) b.clone(); } } $ javac -Xlint t.java t.java:7: clone() has protected access in java.lang.Object blob cpy = (blob) b.clone(); ^ 1 error $
Object.
clone()
implements a shallow copy.
Cloneable
interface,
clone()
as a public method, and
$ cat t.java class blob implementsCloneable
public blob clone() throws CloneNotSupportedException return (blob) super.clone(); class t public static void main(String
args[]) blob b = new blob(); try blob cpy = b.clone(); catch (Exception
e) { } $ javac -Xlint t.java $
Object.clone()
as in a shallow copy, and
clone()
implements the deep copy.
clone()
too.
class blob implementsCloneable
public blob clone() throws CloneNotSupportedException blob b = (blob) super.clone(); // recursively clone instance references. return b; class t public static void main(String
args[]) blob b = new blob(); try blob cpy = b.clone(); catch (Exception
e) { }
Object.
clone()
has the contract
x.clone() != x
x.clone().getClass() == x.getClass()
x.clone().equals(x)
is true.
Object.clone()
appropriately.
clone
judiciously), 16 (Prefer interfaces to
abstract classes), and 17 (Use interfaces only to define types) from
Effective Java by Joshua Bloch, Addison-Wesley, 2001.