Java Object API | Understanding Object Class in Java

Java Object API or the Object class in Java is like the Big Daddy of all classes. It is like the Adam of the entire human race. It is at the top of every hierarchy in Java.

Every class in Java extends Object class unknowingly. It is the parent class of all classes in Java and the topmost in the pyramid. It is the super-super-super….you get the idea, right?

chuck norris meme for java object api

Yep, that should do it. That’s what Object class is.

Reasons to Use Object Class in Java

So when do you really need it?

An Object Class in Java can be used when you are trying to refer to an object but you don’t know the type.

So like there’s a method called whatDoIDo(), and we are not sure about its return type. You can simply put it inside the Object’s reference like this:

Object o = whatDoIDo();

Since upcasting is fine in Java you can use it to even refer to any of its subclass’s object like this:

Object o = new subclass();

where subclass could mean literally any class in Java because they all will extend Object at the end of the day. Then you could use the reference ‘o’ to call some of Object’s methods. Because every object implements the methods of the Java Object API.

It allows all the objects in Java to make use of the good stuff of Object class.

Let’s take a look at the Java Object API in detail that will tell us more about this good stuff.

Java Object API

The Java Object API has its very own set of methods that you can invoke based on your requirement. These have been listed below:

  • clone()
  • equals(Object o)
  • finalize()
  • getClass()
  • hashCode()
  • notify()
  • notifyAll()
  • toString()
  • wait()
  • wait(long timeout)
  • wait(long timeout, int nanos)

We are going to see the important methods here.

Object Cloning in Java

Cloning as you might have guessed already is the act of making an exact copy. You can create an exact copy of an object using Object’s clone() method.

In order to make a clone() of an object, the java.lang.Cloneable Interface must be implemented first or it will give you a CloneNotSupportedException.

Here’s the proper syntax to remember:

protected Object clone() throws CloneNotSupportedException

Okay so then why use clone() when we can do the same by following the following simple method:

ClassName s1 = new ClassName();

ClassName s2 = s1;

See here the thing is that when you use the above method to make a copy, it takes a lot of processing time as compared to using the clone() method. That’s why people often make use of clone() method to copy every member variable as is in a similar object.

Here is an example of clone() method:

public class ItIsAnObject implements Cloneable { 

                  String s;
                  int x;

public ItIsAnObject(String s, int x) { 

                  this.s = s;
                  this.x = x;

 } 

public Object clone() throws CloneNotSupportedException { 

              return Object super.clone(); 

} 
 public static void main(String[] args) {

                  ItIsAnObject s1 = new ItIsAnObject("Why", 23);

                  System.out.println(s1.s + "" + s1.x); 

                  try { 
                  ItIsAnObject s2 = (ItIsAnObject) s1.clone(); 

                  System.out.println(s2.s + "" + s2.x); 

                  } catch (CloneNotSupportedException e) { 
                     e.printStackTrace(); 
                  }
       }
}

If you try to display the result you will get:

Why23
Why23

Meaning the objects have been successfully cloned.

NOTE: We have typecasted s1.clone() method to avoid type mismatch in order to convert it from Object to our own class.

getClass() method

Imagine you are drowned in a gargantuan code. You don’t know the name of the class you are in. And you are too tired to scroll up? What do you do? You call getClass() method to know the name of your class.

Here’s one quick example showing how to achieve that:

public class ItIsAnObject  {

           public static void main(String[] args) {

                   Object o = new ItIsAnObject();

                   System.out.print(o.getClass());

        }

}

You run the above you will get the following result:

class collections.ItIsAnObject

It is a bird. It is a plane. It is an Object!

Don’t worry collections is my package name. It is just how a className is returned, prefixed with the packages.

Other Methods

I have covered the equals() and toString() methods in the String chapter. You can check it out from there.

finalize() is something that is used by the Garbage Collector to check if there are no more references to an object.

hashCode() simply gives you the hash code value which is a 32 bit signed integer.

notify(), notifyAll(), wake() and its variants are all used for multithreading to wake up threads the concept of which have been explained in detail in the multithreading chapter.

I think that sums it all up. It concludes the Big Daddy of classes. You can come up here for reference if you wish to use some of its methods.

Over and out.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

Leave a Reply