Extra information about and relating to Object ------------------------------------------------------------------------------- 1) Clones Clone: + copy object w/o an alias + useful for times when don't want a "lingering" reference + see Clone.txt Want to copy objects...3 ways: + alias: copy reference + shallow: copy top-level of data structure + deep: copy entire data structure Java: + Object's clone() method - inherited method so all objects can make clones of themselves - needs to be overriden, as with equals - http://java.sun.com/j2se/1.4/docs/api/java/lang/Object.html#clone() + Class CloneNotSupportedException - exception thrown when object can't be cloned - see http://java.sun.com/j2se/1.4/docs/api/java/lang/ CloneNotSupportedException.html + Cloneable interface - "Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown" - so, implementing Cloneable interface helps with this irritation - note that Cloneable does NOT have a clone() specification! So, you still need to override Object's clone() - see http://java.sun.com/j2se/1.4/docs/api/java/lang/Cloneable.html ------------------------------------------------------------------------------- 2) Equals Another handy feature of Object is the equals method. What about == ? + if you use == to compare to objects for equality, you will just be testing if to objects have the same address. + == does NOT test for equality of objects' CONTENTS You need .equals + same idea from String, which already defines it + you override Object's equals method to define what you want to be mean for equality + if you do not override, you get equals as ==! See also http://java.sun.com/j2se/1.4/docs/api/java/lang/ Object.html#equals(java.lang.Object) ------------------------------------------------------------------------------- 3) toString text-based representation of an object see http://java.sun.com/j2se/1.4/docs/api/java/lang/Object.html#toString() ------------------------------------------------------------------------------- 4) Class Class You will see this now and then, esp in Object Called REFLECTION, which is a way of a program to recognize and features of itself http://java.sun.com/j2se/1.4/docs/api/java/lang/Class.html examples: + getClass: return an object that represents an entire class (helps you to retrieve name, members, constructors,....) + forName: returns a class based on an input name + getName: return the name of entity (usually a class) as a String -------------------------------------------------------------------------------