A static or non-static field declaration 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 during field hiding.
Field hiding occurs only based on the field name.
class G { protected int x = 200; protected String y = "Hello"; protected double z = 10.5; } 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 }
class MySuper { protected int num = 100; protected String name = "Tom"; } class MySub extends MySuper { public void print() { System.out.println("num: " + num); System.out.println("name: " + name); }//from www .j a v a 2 s. com } class MySub2 extends MySuper { // Hides num field in MySuper class private int num = 200; // Hides name field in MySuper class private String name = "Wally Inman"; public void print() { System.out.println("num: " + num); System.out.println("name: " + name); } } class MySub3 extends MySuper { // Hides the num field in MySuper class private int num = 200; // Hides the name field in MySuper class private String name = "Wally Inman"; public void print() { // MySub3.num System.out.println("num: " + num); // MySuper.num System.out.println("super.num: " + super.num); // MySub3.name System.out.println("name: " + name); // MySuper.name System.out.println("super.name: " + super.name); } } public class Main { public static void main(String[] args) { MySub fhSub = new MySub(); fhSub.print(); MySub2 fhSub2 = new MySub2(); fhSub2.print(); MySub3 fhSub3 = new MySub3(); fhSub3.print(); } }