Consider the following classes :
class MyClass { public void mMyClass (){ }; } class MyClass2 extends MyClass { public void mMyClass (){ } public void mMyClass2 () { } } class MyClass3 extends MyClass2 { public void mMyClass3 (){ } }
and the following declarations:
MyClass x = new MyClass2 (); MyClass2 y = new MyClass2 (); MyClass2 z = new MyClass3 ();
Which of the following calls are polymorphic calls?
Select 3 options
A. x.mMyClass(); B. x.mMyClass2(); C. y.mMyClass(); D. z.mMyClass3(); E. z.mMyClass2();
Correct Options are : A C E
A Polymorphic call means the method of the actual class of the object referred by the variable is invoked.
In java, all non-private method calls are polymorphic.
x.mMyClass2()
is invalid call.
It will not even compile because the class of x is MyClass, which does not contain method mMyClass2()
.
Even though the object referred to by x is of class MyClass2 which does contain mMyClass2()
.
z.mMyClass3()
is invalid for the same reason.