Deep dive into Object class in java ?

Package:
java.lang.Object

About:
Object class is parent of all java classes directly or indirectly.
Directly means if any class does not extends (means parent) any other class, in this case Object class is direct parent.
Indirectly means , if any class extends (means parent) any other class, in this case Object class is super parent of that class indirectly.

Observation:
So, finally Object class is root parent of all java classes.
means here Object Class follows inheritance rule.

Inheritance rule:
What ever properties present in Parent Class those automatically available to Child Class.

So, methods present in Object class available to all java classes and those methods are eligible to
override.

Methods Present in Object Class:
1.equals() method
syntax: public boolean equals(Object obj);
2.hashcode() method
syntax: public int hashCode();
3.getClass() method
syntax: public final Class getClass().;
4.clone() method
syntax: protected Object clone() throws CloneNotSupportedException;
5.toString() method
syntax: public String toString();
6.notify() method
syntax: public final void notify();
7.notifyAll() method
syntax: public final void notifyAll();
8.wait() method
syntax: public final void wait(long timeout) throws InterruptedException;
syntax: public final void wait(long timeout,int nanos) throws InterruptedException;
syntax: public final void wait() throws InterruptedException;
9.finalize() method
syntax: protected void finalize() throws Throwable.

Dive into one by one methods:
1.equals() method
syntax: public boolean equals(Object obj);

equals() method , name it self saying that it is used to compare two objects or two Strings (String also one of java class. so, we can create object for String also, means if you compare 2 Strings means your comparing tow objects that’s it, don’t confuse).

if those tow objects are equal then this equals() method return true , else it returns false.

Note: to meet our requirement , it is recommended to override equals() method.
[ that we will see in Relation between equals() and hashcode() ]

  Code Example:

    public class Skslate{

     public String authorName;

     public void setAuthorName(String authorName){
         this.authorName = authorName;
     } 

     public String getAuthorName(){
         return this.authorName;
     }
    }

    public class MainClass{
        public static void main(String[] args){
            Skslate s1 = new Skslate();
            s1.setName("Kishore");
            Skslate s2 = new Skslate();
            s2.setName("Sanjay");

            System.out.println(s1.equals(s2)); // output will come here
        }

    }

2.hashcode() method
syntax:
public int hashCode();

hashcode() method, if we call to this method it will return hashcode in integer format of current object.

but, initially while creation of objects JVM will generate unique integer number that number we call hashcode.

Mis-Understanding:
but, most of people think hashcode is a address of objects. this is wrong statement.
Basically, internally it converts address of object into some different integer value by using an algorithms.

So, if we call hashcode() method, it will return hash value which is very useful in case of collections.
Before saving object into collections like HashSet, HashMap, Hashtable etc… JVM uses hashcode for saving purpose.

Hashing is very helpful in case of searching , retrival and storing.

Write a comment