The class Object http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html Lecture 10 -- CS1122 Summer 2008
Review Object is at the top of every hierarchy. Every class in Java has an IS-A relationship with Object. Methods of the class Object are available from any reference. Polymorphism allows you to override the default functionality of methods inherited from the class Object.
Object If you go to the Java API, you will see that the class Object has 11 methods. You can override 5 of those methods: equals, hashcode, tostring, clone, finalize We ll talk about the first three, today. To learn about the others, visit: http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html Next week we ll talk about why you can t override some methods.
overriding Object s methods The methods in the class Object are well defined in the API. You must be sure adhere to everything documented in the API. Also think about how your implementation will affect subclasses. Remember, subclasses would use your method before using the default in Object.
overriding Object s methods parameters will be references to Objects this allows a reference to anything to be passed into these methods to make sure you have the right kind of reference, use the getclass() method
getclass() public class Abc{ } private int x = 5; public void doit(object o){ if(o.getclass().equals(this.getclass())){ System.out.println( o references an Abc Obj); }else{ System.out.println( Not an Abc object ); } }
overriding Object s methods parameters will be references to Objects once you know you have a reference to the kind of object you are expecting, you need to use a cast to access parts of that object
casting references public class Abc{ } private int x = 5; public void doit(object o){ if(o.getclass().equals(this.getclass())){ (Abc)o.x = 13; }else{ System.out.println( Not an Abc object ); } }
public boolean equals(object o) Used to test the equality of objects Defaults to the same as == operator tests to see if 2 references point at the same object Override to compare the contents of objects Example: String s = hello ; String t = hello ; if(s.equals(t)){ System.out.println( world ); }
public boolean equals(object o) According to the API, your implementation must be: reflexive: for any non-null reference value x, x.equals(x) should return true. symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
public boolean equals(object o) According to the API, your implementation must be: consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified. For any non-null reference value x, x.equals(null) should return false.
a very brief intro to hashing hashing a fast way of storing/retrieving objects in a Vector, you store objects at a given index in a Hashtable (HashMap, HashSet, etc.), you store objects in a location based on a hash code a hash code is calculated based on the object being stored, instead of being based on the structure it is stored in. hashing is covered in Data Structures class
public int hashcode() this method relies on equals() you should override these methods together According to the API, your implementation must: return the same number for any 2 objects that are equal according to equals() Be careful when using values that can change to calculate the hashcode
public String tostring() by default it prints classname@hashcode this is not very useful in most cases override to give useful information it is recommended that you override this method in every class that you write very helpful for debugging!
Summary the methods in Object exist they are very useful when defined properly the definitions of the methods in Object are very general override methods in Object to provide implementations that are specific to your classes always be sure to follow the definitions given in the API