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 a root of inheritance in Java programs. The 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.

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 the 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 class A { }.

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:

  • threads synchronization: waitnotifynotifyAll;
  • object identityhashCodeequals;
  • object managementfinalizeclonegetClass;
  • human-readable representationtoString;

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: this 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 in the 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.

In the following topics, we will consider these class methods in more detail.

Conclusion

The Object class is a default class in the java.lang package and is a 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 when declaring a class. It provides some common methods to all subclasses, including nine instance methods that are divided into four groups in the present topic for your convenience. Some of these methods are native so you can use them to interface with system calls or other programming language libraries. You will learn about the methods in the next topics.

Leave a Reply

Your email address will not be published.