Object class / Default superclass in Java

The class Object is a superclass of simple class written by you. It sits on the top of the class hirarchy tree. That means every class is a decendant, directly or indirectly of the object class. Therefore every class you use or write all properties and methods of object class. To use these methods you can override these methods in your class code.

Writing a simple student class

is equal to

The follwing methods are available in Object class.

  • protected Object clone() throws CloneNotSupportedException
    Clone method is create copy from an existinng object. To use this method you have to implement Cloneable interface in superclass or in the current class. To create and return a copy of  object you can write :
  • public boolean equals(Object obj)
    The equals method in Object class compares whether some other object is “equal to” this one. The equals() method compares two objects for equality and returns true if they are equal.

  • protected void finalize() throws Throwable
    Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. You can override this method to free the resources.
  • public final Class getClass()
    Returns the runtime class of an object. You can’t override this method. It will simply returns the name of a class.
  • public int hashCode()
    Returns a hash code value for the object which is hexadecimal code.
  • public String toString()
    Returns a string representation of the object. You can override this method to print useful information.

No Responses