Objects, Classes, and Interfaces |
The Object class sits at the top of the class hierarchy tree in the Java development environment. Every class in the Java system is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must have such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class.The equals() Method
Use theequals()
to compare two objects for equality. This method returnstrue
if the objects are equal, false otherwise. Note that equality does not mean that the objects are the same object. Consider this code that tests two Integers,one
andanotherOne
, for equality:This code will displayInteger one = new Integer(1), anotherOne = new Integer(1); if (one.equals(anotherOne)) System.out.println("objects are equal");objects are equal
even thoughone
andanotherOne
reference two different, distinct objects. They are considered equal because they contain the same integer value.Your classes should override this method to provide an appropriate equality test. Your
equals()
method should compare the contents of the objects to see if they are functionally equal and returntrue
if they are.The getClass() Method
ThegetClass()
method is a final method (cannot be overriden) that returns a runtime representation of the class of this object. This method returns a Class object which you can query the Class object for various information about the class such as its name, its superclass, and the names of the interfaces that it implements. This sample method gets and displays the class name of an object:One handy use of thevoid PrintClassName(Object obj) { System.out.println("The Object's class is " + obj.getClass().getName()); }getClass()
is to create a new instance of a class without knowing what the class is at compile time. This sample method creates a new instance of the same class asobj
which can be any class that inherits from Object (which means that it could be any class):Object createNewInstanceOf(Object obj) { return obj.getClass().newInstance(); }The toString() Method
Object'stoString()
method returns a String representation of the object. You can usetoString()
to display an object. For example, you could display a String representation of the current Thread like this:The String representation for an object is entirely dependent on the object. The String representation of an Integer object is the integer value displayed as text. The String representation of a Thread object contains various attributes about the thread, such as its name and priority. For example, the two lines of code above display the following:System.out.println(Thread.currentThread().toString()); System.out.println(new Integer(44).toString());TheThread[main,5,main] 4toString()
method is very useful for debugging and it would behoove you to override this method in all your classes.Object Methods Covered In Other Lessons or Sections
The Object class provides a method,finalize()
that cleans up an object before its garbage collected. This method's role during garbage collection is discussed in this lesson in Cleaning Up Unused Objects. Also, Writing a finalize() Method shows you how to write override thefinalize()
method to handle the finalization needs for you classes.The Object class also provides five methods:
that are critical when writing multithreaded Java programs. These methods help you ensure that your threads are synchronized and are covered in Threads of Control. Take particular note of the page titled Synchronizing Threads.
notify()
notifyAll()
wait()
(three versions)
Objects, Classes, and Interfaces |