<< Chapter < Page | Chapter >> Page > |
p in B
Why is this allowed?
This is allowable because the method named p is declared in the interface named I1 .
Which version of the method was executed?
It is also important to note, (by observing the output) , that the version of the method defined in the class named B (and not the version defined in the class named C ) was actually executed.
Attempt unsuccessfully to call q
Next, the code in Listing 6 attempts, unsuccessfully, to call the method named q on the same reference variable of type I1 .
Listing 6 . Try unsuccessfully to call the method named q. |
---|
var1.q();//won't compile |
Why did it fail?
Even though the class named B , from which the object was instantiated, defines the method named q , that method is neither declared nor inherited into the interface named I1 .
Therefore, a reference of type I1 cannot be used to call the method named q .
The solution is a type conversion
Listing 7 shows the solution to the problem presented by Listing 6 .
Listing 7 . Successfully call the method named q. |
---|
((I2)var1).q();//OK |
As in the case of polymorphism involving class inheritance, the solution is to change the type of the reference to a type that either declares or inherits the method named q .
In this case, this takes the form of using a cast operator to temporarilhy convert the type of the reference from type I1 , to type I2 , and then calling the method named q on that reference of a new type.
This produces the following output:
q in B
Using type I2 directly
Listing 8 instantiates a new object of the class B and stores the object's reference in a reference variable of the interface type I2 .
Listing 8 . Instantiate a new object of the class B. |
---|
I2 var2 = new B();
var2.p();//OKvar2.q();//OK |
Call both methods successfully
Then the code successfully calls both the methods p and q on that reference, producing the following output:
p in B
q in B
Why does this work?
This works because:
Attempt, unsuccessfully, to call x on var2
Following this, the code in Listing 9 attempts, unsuccessfully, to call the method named x on the reference variable named var2 of type I2 . This code produces a compiler error.
Listing 9 . Try unsuccessfully to call the method named x. |
---|
String var3 = var2.x(); |
The object of class B has a method named x
At this point, the reference variable named var2 contains a reference to an object instantiated from the class named B .
Furthermore, the class named B inherits the method named x from the class named A .
Necessary, but not sufficient
However, the fact that the object contains the method is not sufficient to make it executable in this case.
Same song, different verse
The interface named I2 neither declares nor inherits the method named x .
Therefore, the method named x cannot be called using the reference stored in the variable named var2 unless the reference is converted either to type A (where the method named x is defined) or type B (where the method named x is inherited) .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?