Java Reference
In-Depth Information
The first two calls in section #2 are similar to the two calls in section #1. They are bound to the print() method of
the MHidingSub class. The third call, ((MHidingSuper)mhSub).print() , needs a little explanation. The compile-time
type of the mhSub variable is MHidingSub . When you use a typecast ( MHidingSuper ) on the mhSub variable, the
compile-time type of the expression (MHidingSuper)mhSub becomes MHidingSuper . When you call the print()
method on this expression, the compiler binds it to its compile-time type, which is MHidingSuper . Therefore, the
third method call in section #2 is bound to the print() method of the MHidingSuper class.
// #3
mhSuper = mhSub;
mhSuper.print();
((MHidingSub)mhSuper).print();
The first statement in section #3 assigns a reference of MHidingSub object to the mhSuper reference variable.
After the first statement is executed, the mhSuper variable is referring to an object of the MHidingSub class. When the
first call to the print() method is made, the compiler looks at the compile-time type (or declared type) of mhSuper
variable, which is MHidingSuper . Therefore, the compiler binds the call mhSuper.print() to the print() method of
the MHidingSuper class. The second call to the print() method is bound to the print() method of the MHidingSub
class because the typecast (MHidingSub) makes the type of the entire expression as MHidingSub .
a static method of a class cannot hide an instance method of its superclass. If you want to invoke a hidden
method of the superclass from inside a class, you need to qualify the hidden method call with the superclass name.
For example, if you want to call the print() method of the MHidingSuper class from inside the MHidingSub class,
you need to use MHidingSuper.print() . Inside the MHidingSub class, the call to the print() method, without
using the class name or a variable, refers to the hiding method print() of the MHidingSub class.
Tip
Field Hiding
A field declaration ( static or non-static) in a class hides the inherited field with the same name in its superclass. The
type of the field and its access level are not considered in the case of field hiding. Field hiding occurs solely based on
the field name. Early binding is used for field access. That is, the compiler-time type of the class is used to bind the
field access. Consider the following declaration of two classes G and H :
public class G {
protected int x = 200;
protected String y = "Hello";
protected double z = 10.5;
}
public class H extends G {
protected int x = 400; // Hides x in class G
protected String y = "Bye"; // Hides y in class G
protected String z = "OK"; // Hides z in class G
}
The field declarations x , y , and z in class H hide the inherited fields x , y , and z in class G . It is to be emphasized
that the same field name in a class alone hides a field of its superclass. Data types of the hidden and the hiding fields
are immaterial. For example, the data type of z in class G is double , whereas data type of z in class H is String . Still,
 
 
Search WWH ::




Custom Search