by Dinesh Thakur Category: Inheritance

All the classes in Java that you have defined so far are subclasses by default whether or not you have specified the superclass. The class which all classes in Java are descendent of (directly or indirectly) is java.lang.Object class. So each class inherits the instance methods of Object class. It is important to be familiar with the methods provided by the Object class so that you can use them in your class.

Using Object class methods

There are methods in Object class:

Public String toString (): This method returns a string representation of an object. The default implementation returns the name of the class followed by '@' and the hexadecimal representation for the object. You can override this method in your classes, to return your String object for your class.

// Returns a string that lists the host name.
// @, Returns a hexadecimal representation for the object.
public String toString(){
     return getClass().getName()+'@'+Integer.toHexString(dataSource.hashCode());

It is always recommended to override toString() method,returns the desired String representation of Object. So, override of toString() method refer – Overriding toString() in Java
Note : If you print any Object reference, then internally invokes toString() method.

Teacher t = new Teacher();

// Below two statements are equivalent



public boolean equals (Object obj): It compares two objects for equality and returns true if they are equal and false otherwise. The comparison is case-sensitive. The code below is the equals() method in the Object class. The method is checking whether the current instance is the same as the previously passed Object.

public boolean equals(Object obj) {
    return(this== obj);

public final class getClass(): This method returns a class Object (package Java.lang) that is locked by static synchronized methods of the represented class, and that contains information about the object's type, such as its classname, its superclass and the interfaces it implements. It also is used to get metadata of this class.

public class clObject {
static void main(String[] args) {
      Object obj = new String("ecomputernotes");
      Class cl = obj.getClass();
      System.out.println("The returned Class object is the : " + c.getName());

NOTE: The getclass() methods of the Object class declared final since their implementation are essential for proper behavior of an object, and so those methods cannot be overridden by any class.

Public int hashcode (): A hash table is a storage mechanism that creates a key-and-value pair for each element. For example, suppose you are keeping records of employees within a company. You need to develop a mechanism for retrieving these records based on employees' social security numbers. An array really won't work well for this because the nine-digit identification number is too large. (Vectors and arrays work best for an ordered list of sequential values.) In this situation, hash tables are the answer.

Hash tables enable quick location and retrieval of data by using a key. This key is associated with data contained within an object. The key and value can be from any object, but the key object must implement the hashCode() and equals() methods. Once these methods have implemented, the hash table computes a small integer from the key; this is the hash code and the location where the key/value pair exists. Unlike arrays and vectors, the order of values in a hash table is not important-the hash code ID is all that matters. What is important is that the keys are unique: Only one value can store for each key. If you add a value to a key that is already holding another value, then the added value replace the preexisting value.

As with other data structures, the hash table's performance is affected by the ratio between the number of elements and the capacity of the structure. This ratio is called the load factor and represented as a percentage. Hash tables need to allocate more storage space than is needed because they generate the unique hash code from the identifying key. As the load factor increases,
so does the likelihood that two or more generated hash codes have the same value; this is called a collision. The extra space is used to ensure that all key/value pairs remain unique. Ideally, the load factor is not higher than 50%; that is, the capacity should be double the number of elements contained in the hash table. If the load factor goes over 75%, the hash table automatically doubles the capacity to ensure adequate performance.

Protected Object clone() throws CloneNotSupportedException: This method creates a copy of the object on which it is called.

Protected void finalize() throws Throwable: The finalize method is a particular method, declared in the object class which allows cleaning up of objects which are not in use (not referenced).
When any objects no longer reference an object, Java reclaims the memory space using garbage collection. Java calls a finalize method before garbage collection takes place.
void finalize() {
.....body of finalize method

Finalize methods always have a return type of void and override a default destructor in java.object.Object.
A program can call finalize directly just as it would any other method. However, calling finalize not initiate any garbage collection. It treated like any other method called directly. When Java does garbage collection, finalize is still called even if it has already been called directly by the program.
The finalize method can be overloaded also. If Java finds a finalize method with arguments at garbage-collection time, it looks for a finalize method with no arguments. If it does not find one, Java uses the default finalize method instead.
The system only calls finalize when it is ready to reclaim the memory associated with the object and not immediately after an object no longer referenced. That is, finalize() is called only when the system is running short of memory so that it may be called long after the application finishes.

Object Class in Java Example

class Rectangle extends Object

     private double length,breadth;
     Rectangle(double x,double y)
       length = x ;
       breadth = y ;
   public void area()
     System.out.println("Area of Rectangle is = " + breadth);
   public void circumference()
   System.out.println("Circumferen of Rectangle is= "+2*(length+breadth));
 class ObjectClass
     public static void main(String[] args)
           Rectangle r = new Rectangle(10,20);
           Rectangle rl = new Rectangle(10,20);
           System.out.println("String Representation = " + r.toString());
           System.out.println("Class Name = " + r.getClass());
           System.out.println("Hash Code  = " + r.hashCode());
           System.out.println("r.rquals(rl) = " + r.equals(rl));

Object Class in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles