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
Definitely you will say yes and start with toString() method.
References:
Reference 1
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
- public String toString()
- public final native Class getClass()
- public native int hashCode()
- public boolean equals(java.lang.Object)
- protected void finalize() throws Throwable
- protected native Object clone() throws CloneNotSupportedException
- public final native void notify()
- public final native void notifyAll()
- public final native void wait(long) throws java.lang.InterruptedException
- public final void wait(long, int) throws java.lang.InterruptedException
- public final void wait() throws java.lang.InterruptedException
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 exampleReferences:
Reference 1
Sponsored Links
0 comments:
Post a Comment