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)



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.


public class MyClass implements Cloneable{

  int id;

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

    MyClass mc = new MyClass();






This will print: 10

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



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())



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.



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.



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.



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.



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.




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

Was it useful?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)