Monday, 19 June 2017

Object class in Java

Object class is root class of all classes in Java. All classes descend from object directly or indirectly. It is implicitly extended by a class if it is not extending any other class. In case, a class is extending any other class then also it extends object as a multi-level hierarchy inheritance.

As example if we have a class Employee

public class Employee {

}

It is same as

public class Employee extends Object {

}

Since it is implicitly extended by a class so you don’t need to write explicitly that your class is extending Object class.

Methods in Object class

Every class you use or write inherits the instance methods of Object. In your class you may not need to use any of these methods. In case you need to use methods inherited from the Object class you may need to override them with code that is specific to your class.

Method Description
clone()Creates and returns a copy of this object.
boolean equals(Object obj)Indicates whether some other object is "equal to" this one.
protected void finalize()Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
Class<?> getClass()Returns the runtime class of this Object.
int hashCode()Returns a hash code value for the object.
void notify()Wakes up a single thread that is waiting on this object's monitor.
void notifyAll()Wakes up all threads that are waiting on this object's monitor.
String toString()Returns a string representation of the object.
void wait()Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
void wait(long timeout)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.
void wait(long timeout, int nanos)Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

The clone() Method

If a class, or one of its superclasses, implements the Cloneable interface, you can use the clone() method to create a copy from an existing object. To create a clone, you write:

cloneableObject.clone();

The equals() and hashCode() Method

The equals() method compares two objects for equality and returns true if they are equal.

The value returned by hashCode() is the object's hash code, which is the object's memory address in hexadecimal. If you override the equals() method, you must also override the hashCode() method as well.

The finalize() Method

The Object class provides a callback method, finalize(), that may be invoked on an object when it becomes garbage. Though Object's implementation of finalize() does nothing and you have to override finalize() to do cleanup, such as freeing resources.

The getClass() Method

The getClass() method returns a Class object, which has methods you can use to get information about the class, such as its name (getSimpleName()), its superclass (getSuperclass()), and the interfaces it implements (getInterfaces()).

Note that you cannot override getClass method.

The toString() Method

The Object's toString() method returns a String representation of the object, which is very useful for debugging.

You should always consider overriding the toString() method in your classes. IN your class. You can use toString() along with System.out.println() to display a text representation of an object. In System.out.println() you can provide fields of an object to get all the values and verify those values.

wait() and notify() methods

wait() method causes the currently executing thread to wait.

notify() method wakes up a single thread waiting on this object’s monitor.

Advantages of Object class

  • Since Object class is the super class for all the classes so Object class can be used as a parameter or reference for any class object which helps in making your code more generic and following object oriented principles like polymorphism and dynamic method dispatch.
  • Though with introduction of Generics in Java 5, Object class has lost some of its sheen and now it is not that ubiquitous in code as it used to be. Before generics all collection were storing objects inherently as Object and we needed to typecast them (by downcasting from Object class reference to your class object) to the custom object while retrieving them.
  • Object class also provides some methods that are applicable to all classes and provides basic implementation too which can be overridden by the class wanting to use those methods.

That's all for this topic Object class in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. instanceof Operator in Java
  2. String in Java
  3. Type erasure in Java Generics
  4. Why wait(), notify() and notifyAll() must be called inside a synchronized method or block?
  5. final Vs finally Vs finalize

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment