Home : Course Map : Chapter 4 : Java :
Object Class
JavaTech
Course Map
Chapter 4

Introduction
Inheritance
  Demo 1
Overriding
  Demo 2a
  Demo 2b
this,super
MoreConstructors
  Demo 3
Abstraction
Interface 
  Demo 4
Casting References
MoreAboutArrays
Object class
Class Summary
Exercises

    Supplements
Override/Overload
Annotation-J2SE5.0
Java Security
Class Loading
Class Verifier
SecurityManager
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

All Java objects derive from the Object base class. These is true even though you don't explicitly include the "extend Object" in your class definition. For example,

  public class Test
  {
    ...
  }

is equivalent to

  public class Test extends Object
  {
    ...
  }

This means that all Java objects are of the Object type. This allows for polymorphic referencing throughout. For example, you can create an array of Object type and the elements can then reference a mix of Java objects (but not primitive types since they are not objects.) When an element in the array is obtained, it can then be cast to its particular subclass type.

The instanceof operator can test for the type of class as in

public void miscMethod(Object obj) {
   if (obj instanceof AClass) ((AClass)obj).aMethod ();
   if (obj instanceof BClass) ((BClass)obj).bMethod ();
   if (obj instanceof CClass) ((CClass)obj).cMethod ();
}

In addition, the Object class provides several methods useful to all of its subclasses (see the API Specification). These methods can also be overriden to provide operations specific to a particular subclass.

These methods include:

  • clone () - produces copies of an object.

  • equals(Object obj) - tests whether an object is equal to the object obj. The default is to test simply whether obj references the same object, not whether two independent objects contain identical properties. This method is often overriden as in the String class, which will test whether the strings actually match.

  • toString () - see discussion below.

  • finalize ()- called by the garbage collector when there are no more references to this object. You can override this method to take care of any housecleaning operations needed before the object disappears.

  • getClass () - gets the runtime class name of the object - see the Class class in the Chapter 5: Supplements.

  • hashCode () - generate a hash code value unique for this object.

  • The following methods involve thread synchronization that will be briefly disussed in Chapter 8: Threads. They can only be called from within a synchronized method or code block:

    • notify () - called by a thread that owns an object's lock to tell a waiting thread, as arbitrarily chosen by the VM, that the lock is now available.

    • notifyAll () - similar to notify() but wakes all waiting threads and then they compete as usual for the lock.

    • wait () - the thread that owns the lock on this object will release the lock and then wait for a notify() or notifyAll() to get the lock back.

    • wait (long msecs) - same as wait() but if a notify fails to come within the specified time, it wakes up and starts competing for the lock on this object.

    • wait (long msecs, int nanosecs) - same as wait(long msecs) but specified to the nanosec.
  •  

Note that most OS systems are not accurate to a nanosecond and some not even to a few milliseconds.

Objects to Strings

We discussed in Chapter 3 how to convert primitive types to and from strings. You can also convert any Java object to a string since all objects inherit the toString() method from the Object class.

The default version of toString() will produce a string beginning with the class name and data values appended to it. However, usually the toString() method is overridden with a method that provides a string output in a more readable format customized to the data for that class.

You can call the toString() method directly, or, alternatively, the "+" operator will call the toString() method whenever the variable refers to an object. For example, in

     Double aD = 5.0;
     String aDStr = "d = " + D;

the plus operator in the second line invokes the toString()method of the Double object ad and so results in aDStr referencing the string "d = 5.0".

 

References & Web Resources

 

Latest update: Oct.24, 2004

            Tech
MoreComplexClass
ImprovedHistogram
JavaRandomNums
Vectors & Matrices
Exercises

           Physics
Runge-Kutta 2nd
  Demo 1
Runge-Kutta 4th
  Demo 2
BoundaryVal.Prob
Shooting Method
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.