The Object Class Tutorial

Every class you will ever create is derived at some point from the Object class. It is the granddaddy of all Java objects. If you create a class and you do not specifically extend a superclass, then Java implicitly extends the Object class as a superclass for you. In the Object class there are eleven methods: eight of them are regular no-argument methods, and then there is the wait() method which has two more overloaded versions. All of the methods in the Object class are automatically inherited and available for use in every class that we create; we can also override some of these methods as well. I will go over each and every one of these methods in future tutorials. As a matter of fact, I already created a tutorial on one of the methods without telling you - the finalize() method. When I created my tutorial on Garbage Collection, I thought it would be a good opportunity to make a tutorial on the Finalize Method because the two are closely related.
In this tutorial, I will take you to the Oracle website and we will browse around the Object class page for a little while. For the code example, I will simply invoke the toString() method.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md TheObjectClass
C:\Java>cd TheObjectClass
C:\Java\TheObjectClass>Notepad TheObjectClass.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class TheObjectClass {
    public static void main(String args[]) {
        Acme acmeRef = new Acme();
        String myString = acmeRef.toString();
        System.out.println("myString = " + myString);
        System.out.println("acmeRef = " + acmeRef);
    }
}

class Acme {    
}

Now switch back to the command prompt (CMD) and type in javac TheObjectClass.java and press Enter.
Now type in java TheObjectClass and press Enter.


C:\Java\TheObjectClass>javac TheObjectClass.java
C:\Java\TheObjectClass>java TheObjectClass
myString = Acme@659e0bfd
acmeRef = Acme@659e0bfd


Final thoughts

Over time you will come to learn the purpose of each and every method in the Object class. The Object class is the original building block for the Java language. Having a clear understanding of the Object class will be necessary to comprehend advanced concepts.


Tutorials