[ACCEPTED]-What happens when base and derived classes each have variables with the same name-java

Accepted answer
Score: 26

There are actually two distinct public instance 29 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();

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

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

The technical term 25 for what is happening here is "hiding". Refer 24 to the JLS section 8.3, and section for an example.

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

However instance methods 21 with the same signature are "overridden" not 20 "hidden", and you cannot access 19 the version of a method that is overridden 18 from the outside. (Within the class that 17 overrides a method, the overridden method 16 can be called using super. However, that's the 15 only situation where this is allowed. The 14 reason that accessing overridden methods 13 is generally forbidden is that it would 12 break data abstraction.)

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

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

Score: 4

From JLS Example: Hiding of Instance 30 Variables This example is similar to that 29 in the previous section, but uses instance 28 variables rather than static variables. The 27 code:

class Point {
  int x = 2;
class Test extends Point {
  double x = 4.7;
  void printBoth() {
      System.out.println(x + " " + super.x);
  public static void main(String[] args) {
      Test sample = new Test();
      System.out.println(sample.x + " " + 

produces the output:

4.7 2
4.7 2

because the declaration 26 of x in class Test hides the definition 25 of x in class Point, so class Test does 24 not inherit the field x from its superclass 23 Point. It must be noted, however, that 22 while the field x of class Point is not 21 inherited by class Test, it is nevertheless 20 implemented by instances of class Test. In 19 other words, every instance of class Test contains 18 two fields, one of type int and one of 17 type double. Both fields bear the name 16 x, but within the declaration of class 15 Test, the simple name x always refers 14 to the field declared within class Test. Code 13 in instance methods of class Test may refer 12 to the instance variable x of class Point 11 as super.x.

Code that uses a field access expression 10 to access field x will access the field 9 named x in the class indicated by the 8 type of reference expression. Thus, the 7 expression sample.x accesses a double 6 value, the instance variable declared 5 in class Test, because the type of the 4 variable sample is Test, but the expression ((Point)sample).x 3 accesses an int value, the instance variable 2 declared in class Point, because of the 1 cast to type Point.

Score: 2

In inheritance, a Base class object can 16 refer to an instance of Derived class.

So 15 this is how Foo f = new Bar(); works okay.

Now when f.addFive(); statement 14 gets invoked it actually calls the 'addFive() method 13 of the Derived class instance using the 12 reference variable of the Base class. So 11 ultimately the method of 'Bar' class gets 10 invoked. But as you see the addFive() method of 'Bar' class 9 just prints 'b ' and not the value of 'a'.

The 8 next statement i.e. System.out.println(f.a) is the one that actually 7 prints the value of a which ultimately gets 6 appended to the previous output and so you 5 see the final output as 'b 3'. Here the 4 value of a used is that of 'Foo' class.

Hope 3 this trick execution & coding is clear 2 and you understood how you got the output 1 as 'b 3'.

More Related questions