Celsius' Notes
Hire me as a freelancer for your mobile (iOS native, cross-platform with React Native), web and backend software needs.
Go to portfolio/resumé
2 min read

The Java Object class

In Java all classes inherit from the objects class. This means that each and every last class in Java has the object class in its inheritance chain. This sets Java apart among object oriented languages most of which do not have a single base class from which all other classes inherit. Inheriting from the Object class isn’t something you as a programmer have to think about when writing your own classes though, as this is done by the compiler automatically. So when you write a new Java class, it automatically extends Object without you specifically having to write that in the class declaration.

What's in it

The Object class implements some useful methods that can be used as is by all other classes or can be overridden by them. The following methods are an incomplete overview of some of those methods

hashCode()

The hashCode() method simply returns the address of the object on which it is called in hexadecimal. While it’s not much of a hashing algorithm, it works since every object is guaranteed to have a unique address. You can override this method to provide your own implementation of it.

equals()

When working with objects in OOP languages you must make the distinction between equality and identity. Identity means that two objects are one and the same, so they are in fact not two objects but actually only one. This means that when have two variables a and b, identity refers to a and b referencing the exact same object in memory. Equality ,on the other hand, refers to two entities being equivalent. The equals method implements an equivalence relation between two objects. This is application and scenario dependent. Two strings, for instance, are equal if there made up of the same characters and the cases match on all those characters. Those two String objects are not necessarily identical however, since they could still be two different string objects, which simply have the same set of character in the same order. The equals function is used, you guessed it, for determining the equality if two objects of the same type. The standard implementation of equals() simply uses the hash codes returned by the hashCode() method to determine if two objects are equal. Since the value returned by hashCode() is the address of an object the standard equals()implementation actually checks for identity of two objects. As with hashCode(), you can override the equals function and provide your own implementation. If you do so, you should also provide your own implementation of the hashCode() method so that equal objects according to your own implementation also have the same hash codes and objects which are not equal do not (It is not required that two objects considered unequal according to your equals() implementation return distinct hash codes, it is however recommended as it can improve the performance of hash tables )

toString()

The toString() methods returns a string representation of the object in question. The standard toString() implementation returns the classname of which the object is a instance followed by the @ sign followed by the unsigned hexadecimal representation of the hash code of the object. If you decide to override the methods and provide your own implementation you should return a concise but informative string representation of your object.

There are more methods in the object class, some of which cannot be overridden. If you find yourself having to override any of the methods provided by the object class make sure to keep the implementations consistent and honoring the contracts described in the Java documentation for those methods.