What happens when base and derived classes each have variables with the same name

There are actually two distinct public instance variables called a.

  • A Foo object has a Foo.a variable.
  • A Bar object has both Foo.a and Bar.a variables.

When you run this:

    Foo f = new Bar();
    f.addFive();
    System.out.println(f.a);

the addFive method is updating the Bar.a variable, and then reading the Foo.a variable. To read the Bar.a variable, you would need to do this:

    System.out.println(((Bar) f).a);

The technical term for what is happening here is “hiding”. Refer to the JLS section 8.3, and section 8.3.3.2 for an example.

Note that hiding also applies to static methods with the same signature.

However instance methods with the same signature are “overridden” not “hidden”, and you cannot access the version of a method that is overridden from the outside. (Within the class that overrides a method, the overridden method can be called using super. However, that’s the only situation where this is allowed. The reason that accessing overridden methods is generally forbidden is that it would break data abstraction.)


The recommended way to avoid the confusion of (accidental) hiding is to declare your instance variables as private and access them via getter and setter methods. There are lots of other good reasons for using getters and setters too.


It should also be noted that: 1) Exposing public variables (like a) is generally a bad idea, because it leads to weak abstraction, unwanted coupling, and other problems. 2) Intentionally declaring a 2nd public a variable in the child class is a truly awful idea.

Leave a Comment