Java Reference
In-Depth Information
System.out.println("#1");
// #1
MHidingSuper.print();
mhSuper.print();
System.out.println("#2");
// #2
MHidingSub.print();
mhSub.print();
((MHidingSuper) mhSub).print();
System.out.println("#3");
// #3
mhSuper = mhSub;
mhSuper.print();
((MHidingSub) mhSuper).print();
}
}
#1
Inside MHidingSuper.print()
Inside MHidingSuper.print()
#2
Inside MHidingSub.print()
Inside MHidingSub.print()
Inside MHidingSuper.print()
#3
Inside MHidingSuper.print()
Inside MHidingSub.print()
The test code has three sections labeled
#1
,
#2
, and
#3
. Let's discuss how early binding is performed by the
compiler in each section.
// #1
MHidingSuper.print();
mhSuper.print();
The first call,
MHidingSuper.print()
, is made using a class name. The compiler binds this call to execute the
print()
method of the
MHidingSuper
class. The second call,
mhSuper.print()
, is made using the reference variable
mhSuper
. The compile-time type (or declared type) of the
mhSuper
variable is
MHidingSuper
. Therefore, the compiler
binds this call to execute the
print()
method of the
MHidingSuper
class.
// #2
MHidingSub.print();
mhSub.print();
((MHidingSuper)mhSub).print();