MY mENU


Wednesday, 30 January 2013

Object Class


The object class is the root class of  the class hierarchy.

Why object class is the super class for all java classes?

Because of below 2 reasons object class is made as super class for every class.
Reusability: every object has 11 common properties. These properties must be implemented by every class developer. So to reduce burden on developer SUN developed a class called object by implementing all these 11 properties with 11 methods. All these 11 methods have generic logic common for all subclasses. If this logic is not satisfying subclass requirement then subclass can override it.

To achieve runtime polymorphism: so that we can write single method to receicve and send any type of class object as argument and as return type.

What are the common functionalities of every class object?

1. Comparing two objects:
Public Boolean equals(Object obj)

2. Retrieving hashcode(object identity)
Public native int hashCode()

3. Retrieving the runtime class object reference:
Public final native Class getClass()

4. Retrieving object information in String format for printing purpose:
Public String toString()

5. Cloning Object:
Protected native Object clone() throws CloneNotSupportException

6. Executing object clean-up code/ resource releasing code:
Protected void finalize() throws Throwalbe

7.8,9: Releasing object lock and sending thread to waiting state:
Public final void wait() throws InterruptedException
Public final native void wait(long millis) throws InterruptedException
Public final void wait(long millis,int nanos) throws InterruptedException

10,11:.Notify about object lock availability to waiting threads:
Public final native void notify()
Public final native void notifyAll()

All above operations are implemented with generic logic that is common for all objects, so that developer can avoid implementing these operation in every class, also SUN can ensure that all the operations are overridden by developers with the same method prototype. This feature will provide runtime polymorphism.

Developer should override these methods with object state.

We can override below five methods:
  1. Equals
  2. hashCode
  3. toString
  4. clone
  5. finalize


sun recommended to override equals,hashCode,toString method in all classes.


No comments:

Post a Comment