Java Object class

The Java Standard Library has a class named Object that is the default parent of all standard classes and your custom classes. Every class extends this one implicitly, therefore it's the root of inheritance in Java programs. The Object class belongs to the java.lang package that is imported by default.

Instantiating the Object class

Let's create an instance of the Object class in this code example:

Object anObject = new Object();

The Object class can refer to an instance of any class because any instance is a kind of Object (upcasting).

Long number = 1_000_000L;
Object obj1 = number; // an instance of Long can be cast to Object

String str = "str";
Object obj2 = str; // the same with an instance of String

When we declare a class, we can explicitly extend the Object class. However, there is no point, since the extension is already done implicitly. We advise you to avoid redundancy in your code, but here's an example, just in case:

class A extends Object { }

In your own solutions, it is enough to write classA{}.

Methods provided by the Object class

The Object class provides some common methods to all subclasses. It has nine instance methods (excluding overloaded methods) which can be divided into four groups:

  • thread synchronization — waitnotifynotifyAll;
  • object identity — hashCodeequals;
  • object management — finalizeclonegetClass;
  • human-readable representation — toString;

This way of grouping methods isn't perfect, but it can help you remember them. Here's a more detailed explanation of the methods:

  • The first group of methods (waitnotifynotifyAll) are for working in multithreaded applications.
  • hashCode returns a hash code value for the object.
  • equals indicates whether some other object is equal to this particular one.
  • finalize is called by the garbage collector (GC) on an object when the GC wants to clean it up. (Note: garbage collection method has been deprecated as of JDK 9).
  • clone creates and returns a copy of the object.
  • getClass returns an instance of Class, which has information about the runtime class.
  • toString returns a string representation of the object.

Some of the methods listed above are native, which means they are implemented using native code. It is typically written in C or C++. Native methods are usually used to interface with system calls or libraries written in other programming languages.

Conclusion

Java is an object-oriented programming language where classes and objects are basic concepts in OOP. The Object in Java is a default class in the java.lang package and is the root of inheritance in Java programs. Every instance of any class is a kind of Object so there is no need to explicitly extend it in class declaration. It provides some common methods to all subclasses, including nine instance methods that are divided into four types for your convenience. Some of these methods are native, so you can use them to interface with system calls or other programming language libraries. These methods are used across multiple times within Java applications, helping ensure consistency and reusability.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate