<< Chapter < Page Chapter >> Page >

References of type Object

The classes mentioned above store references to objects created according to interfaces, contracts, and stipulations provided by the framework. More importantly for the purposes of this module, those references are stored as type Object . (See Java4210: Getting Started with Generics for additional information on this topic.)

The Object type is a completely generic type, which can be used to store a reference to any object that can be instantiated in Java.

Methods defined in the Object class

In an earlier module, I told you that the class named Object defines default versions of the following methods:

  • clone()
  • equals(Object obj)
  • finalize()
  • getClass()
  • hashCode()
  • notify()
  • notifyAll()
  • toString()
  • wait()
  • wait(long timeout)
  • wait(long timeout, int nanos)

Every class inherits these methods

Because every class is either a direct or indirect subclass of Object , every class in Java, (including new classes that you define) , inherits these eleven methods.

To be overridden ...

Some of these eleven methods are intended to be overridden for various purposes.

Calling methods of the Object class

You can store a reference to any object in a reference variable of type Object .

If you have studied the previous modules in this collection, you also know how runtime polymorphism based on class inheritance works.

Given the above, you should know that you can call any of the methods defined in the Object class on any reference to any object stored in a reference variable of type Object (including the references stored in the concrete implementations of the Java Collections Framework) .

And the behavior will be ...

If the class from which that object is instantiated inherits or defines an overridden version of one of the methods in the above list, calling that method on the reference will cause the overridden version to be executed.

Otherwise, calling that method on the reference will cause the default version defined in the Object class to be executed.

A sample program

This is illustrated in the program named Poly04 , which you can view in its entirety in Listing 7 near the end of this module.

For purposes of illustration, this program deals specifically with the method named toString from the above list, but it could deal just as well with other non-final methods in the list.

The class named A

Listing 1 defines a class named A , which extends the class named Object (recall that it is not necessary to explicitly show that a class extends Object ).

Listing 1 . Definition of the class named A.
class A extends Object{ //This class is empty}//end class A

Does not override the toString method

The most important thing to note about the class named A is that it does not override any of the methods that it inherits from the class named Object .

For purposes of this illustration, we will say that it inherits the default version of the method named toString , from the class named Object . (We will see an example of the behavior of the default version of the toString method shortly.)

The class named B

Listing 2 contains the definition of a class named B . This class extends the class named A .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask