Sponsored Links

Ad by Google
This question is most respected java interview question and always on demand, before going to advance concept smart interviewer wants to check your fundamental concept regarding java.

Interviewer ask you to list down all the method of Object class or list down all the public non final methods of Object class, once you listed he/she will start asking about why they are in object class and when to use them?
Well in java Object class is a super class of all the classes available in java including your own classes and resides inside java.lang package. All classes including your own custom classes inherit the instance methods of Object class and those methods are listed below. type javap java.lang.Object on command prompt to list all the methods of Object class.

Here we listed all the methods of java.lang.Object class
  1. public String toString()
  2. public final native Class getClass()
  3. public native int hashCode()
  4. public boolean equals(java.lang.Object)
  5. protected void finalize() throws Throwable
  6. protected native Object clone() throws CloneNotSupportedException
  7. public final native void notify()
  8. public final native void notifyAll()
  9. public final native void wait(long) throws java.lang.InterruptedException
  10. public final void wait(long, int) throws java.lang.InterruptedException
  11. public final void wait() throws java.lang.InterruptedException
Once you listed the above methods. Now you will enter into the second question via can you elaborate the public non final methods such as toString(),hashCode(),equals(obj).
Definitely you will say yes and start with toString() method.

public String toString()

Well toString() method always return a String representation of the Object. The Default implementation of toString() method's return String is fully qualified name of the class which the object is an instance concatenating with @ symbol and hex String of the hash code return by the hashCode() method. In short return String of the default implementation of toString() method is not in human readable format. That is why you should always need to override the toString() method. For more on how toString() method works.

public int hashCode()

The hashCode() method is a native method so the default implementation of hashCode() method is in native code, available somewhere inside the JVM. The integer number return by the hashCode() method is the internal memory address of the object and it's useful when using the objects in a hash based collections or other algorithms that rely on hash codes such as HashMap, HashSet and Hashtable. For more on why hashCode() method in java?

public boolean equals(java.lang.Object)

The equals() method as it's name implies this method is used to check the equality of the objects and you know the equality is possible only with more than one objects. So the same way, equals() method of Object class is used to check whether "this object" is equal to "other object". Here is more detailed with example

Reference 1
Sponsored Links


Post a Comment