<< Chapter < Page Chapter >> Page >
Listing 1 . Definition of the class named A.
class A extends Object{ public void m(){System.out.println("m in class A"); }//end method m()}//end class A

The class named A defines a method named m() .

Behavior of the method

The behavior of the method, as defined in the class named A , is to display a message indicating that it has been called, and that it is defined in the class named A .

This message will allow us to determine which version of the method is executed in each case discussed later.

The class named B

Listing 2 shows the definition of a class named B that extends the class named A .

Listing 2 . Definition of the class named B.
class B extends A{ public void m(){System.out.println("m in class B"); }//end method m()}//end class B

The class named B overrides (redefines) the method named m() , which it inherits from the class named A .

Behavior of the overridden version of the method

Like the inherited version, the overridden version displays a message indicating that it has been called. However, the message is different from the message displayed by the inherited version discussed above. The overridden version tells us that it is defined in the class named B . (The behavior of the overridden version of the method is appropriate for an object instantiated from the class named B .)

Again, this message will allow us to determine which version of the method is executed in each case discussed later.

The driver class

Listing 3 shows the beginning of the driver class named Poly03 .

Listing 3 . Beginning of the driver class named Poly03.
public class Poly03{ public static void main(String[]args){ Object var = new B();((B)var).m();

A new object of the class B

The code in the main method begins by instantiating a new object of the class named B , and assigning the object's reference to a reference variable of type Object .

(Recall that this is legal because an object's reference can be assigned to any reference variable whose type is a superclass of the class from which the object was instantiated. The class named Object is the superclass of all classes.)

Downcast and call the method

If you read the earlier module on casting, it will come as no surprise to you that the second statement in the main method, which casts the reference down to type B and calls the method named m() on it, will compile and execute successfully.

Which version is executed?

The execution of the method produces the following output on the computer screen:

m in class B

By examining the output, you can confirm that the version of the method that was overridden in the class named B is the version that was executed.

Why was this version executed?

This should also come as no surprise to you. The cast converts the type of the reference from type Object to type B .

You can always call a public method belonging to an object using a reference to the object whose type is the same as the class from which the object was instantiated.

Not runtime polymorphic behavior

Just for the record, the above call to the method does not constitute runtime polymorphism (in my opinion) . I included that call to the method to serve as a backdrop for what follows.

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