Object Class - The Parent Class of All Classes in Java

There is a class in Java called Object and is present in the package java.lang.Object. This is different from the concept of objects in object oriented programing, and begenners may get confused. Note that is is just a class in Java with the name as Object. 

 

Object class API basics

  • Class java.lang.Object is the root of the class hierarchy in Java.

  • Every class has Object as a superclass either directly or indirectly.

  • All objects, including arrays, extends from the Object class and implement the methods of this class.

  • Object is also a concrete class.

  • Object class has one parameter less constructor Object() that does nothing.

 

Methods of Object class

Object class has below methods:

  1. protected Object clone()

  2. Class<?> getClass()

  3. protected void finalize()

  4. String toString()

  5. Boolean equals(Object obj)

  6. int hashCode()

  7. void notify()

  8. void notifyAll()String toString()

  9. void wait()

  10. void wait(long timeout)

  11. void wait(long timeout, int nanos)

 

clone()

Method clone() creates and returns a copy of the object on which it is called,  though the exact behavior may depend on the class of the object. By convention, the returned object should be obtained by calling super.clone, with an assumption that all of its superclasses (except Object) obey this convention. Also, by convention, the object returned by this method should be independent of the object on which it is called, and for that we may have to modify one or more fields of the object returned by super.clone, like deep copying (copying any mutable object members of the object being cloned and replacing the references to these objects with references to the copies). If a class contains only primitive fields or references to immutable objects, then usually no fields in the object returned by super.clone need to be modified.

If the class of object on which it is called does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Cloneable is a marker interface (no methods to implement). Arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type. Otherwise, clone() creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned (which is shallow copy).

The class Object does not implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.

Example:

public class MyClass implements Cloneable{

  int id;

  public static void main(String[] args) throws Exception {

    MyClass mc = new MyClass();

    mc.id=10;

    System.out.println(((MyClass)mc.clone()).id);

  }

}

Answer

This will print: 10

mc.clone will do a shallow copy of the object represented by mc.

 

toString()

Method toString returns a string representation of the object.

The toString method for class Object returns a string consisting of the name of the class, the `@' character, and the unsigned hexadecimal representation of the hash code of the object. You can get the same value using below statement:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

 

getClass()

Method getClass returns the runtime class of this Object as a Class object. Instances of the class Class represent classes and interfaces in a running Java application. It had methods to know more about the class type and its members. For instance, the isArray() method will check if the object represent an array or not. The static synchronized methods of a class lock on the Class object for that class. We will see more about getClass when we will discuss reflection.

 

finalize()

Method finalize will be called by GC when the object is eligible for garbage collection. Main purpose of this method is to perform any cleanup actions before the object is completely discarded by GC. However there are many reasons why you should generally not use it. You can read more about finalize @ http://javajee.com/finalize-method-in-java-why-not-to-use-limited-use-cases-and-alternatives.

The finalize method of class Object performs no special action; it simply returns normally.

 

equals()

Method equals, in simple terms, check if two objects are equal. String class and most collection classes overrides equal to check if two objects are meaningfully equal, which means, if two different string objects have same value. The equals method for class Object will return true, for any non-null reference values x and y, if x and y refer to the same object. We will see more about equals when we will see collections.

 

hashCode()

Method hashCode returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap. The hashCode method is usually used along with equals.

The hashCode method has a general contract as given below:

  1. Whenever hashCode method is invoked on the same object more than once during one execution of a Java application, it should return the same integer, provided no information used in equals comparisons on the object is modified.

  2. If two objects are equal according to the equals(Object) method, then the hashCode method should also return same hash code integer when invoked on each of the two objects.

  3. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then the hashCode should differ. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

We will see more about hashCode  when we will see collections.

 

notify()

Method notify wakes up one of the thread that is waiting on this object's monitor. Method notifyAll wakes up all threads that are waiting on this object's monitor. The wait methods causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.  Though these methods are used in the context of threading, this is part of the Object class as you can acquire lock monitor on any object. We will see more about wait, notify and notifyAll when we will see multithreading.

 

Quizzes

Comments

Object class is the best friendly class for all java programmers ... it's really nice to know about it.

Was it useful?

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!