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; }
class Universals { private 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)
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.
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()
.
clone()
too.
Object.clone()
appropriately.