<< Chapter < Page Chapter >> Page >

Do the type conversion

The required type conversion is accomplished in Listing 10 where the reference is temporarily converted to type A using a cast operator. (It would also work to cast it to type B .)

Listing 10 . Successfully call the method named x.
String var3 = ((A)var2).x();//OK System.out.println(var3);

The String produced by the first statement in Listing 10 is passed to the println method causing the following text to be displayed on the computer screen:

x in A

Get ready for a surprise

If you have now caught onto the general scheme of things, the next thing that I am going to show you may result in a little surprise.

Successfully call the toString method on var2

The first statement in Listing 11 successfully calls the toString method on the object of the class B whose reference is stored as type I2 .

Listing 11 . Call the toString method.
var3 = var2.toString();//OK System.out.println(var3);

How can this work?

How can this work when the interface named I2 neither declares nor inherits a method named toString .

A subtle difference in behavior

I am unable to point you to any Oracle documentation to verify the following. (I also admit that I haven't spent a large amount of time searching for such documentation).

With respect to the eleven methods declared in the Object class (listed in an earlier module) , a reference of an interface type acts like it is also of type Object .

And the end result is ...

This allows the methods declared in the Object class to be called on references held as interface types without a requirement to cast the references to type Object . (Later, I will show you that the reverse is not true.)

The output

Therefore, the two statements shown in Listing 11 cause the following to be displayed on the computer screen:

toString in A

Polymorphism applies

Note that the object whose reference is held in var2 was instantiated from the class named B , which extends the class named A .

Due to polymorphism, the toString method that was actually executed in Listing 11 was the overridden version defined in class A , and not the default version defined in the Object class. The overridden version in class A was inherited into class B .

The reverse is not true

While a reference of an interface type also acts like type Object , a reference of type Object does not act like an interface type.

Store a reference as type Object

The code in Listing 12 instantiates a new object of type B and stores it in a reference of type Object .

Attempt unsuccessfully to call p

Then it attempts, unsuccessfully, to call the method named p on the reference.

Listing 12 . Try unsuccessfully to call the method named p.
Object var4 = new B(); var4.p();//won't compile

Same song, an even different verse

The code in Listing 12 won't compile, because the Object class neither defines nor inherits the method named p .

In order to call the method named p on the reference of type Object , the type of the reference must be changed to either:

  • The class in which the method is defined
  • An interface that declares the method, which is implemented by the class in which the method is defined
  • A couple of other possibilities involving subclasses or sub-interfaces

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