2

Hello I found a question like this. The final answer printed by the main method is 3. But what I feel is the answer should be 13. When the new Bar() object is created, its constructor is called. the super() first line, would call back the Super method's constructor and assign a as 3. When call returns back to Bar() constructor "a" will be assigned to 8. Then after invoking addFive() this.a will be 13. So finally at the print statement by using the object Bar() referenced by n, calls a. Isn't it should be 13. How did the super calss "a" value print? please someone help me to sort it out.

 public class _416 {
        public int a;
        public _416(){
        a=3;
        }
        public void addFive(){

        a+=5;
        }
    }
    class Bar extends _416{

        public int a;
        public Bar(){
        a=8;
        }

        public void addFive(){
        this.a+=5;
        }
        public static void main(String[] args) {
            _416 n=new Bar();
            n.addFive();
            System.out.println("Value="+n.a);
        }
    }
user3789200
  • 1,166
  • 2
  • 25
  • 45

2 Answers2

1

Variables are not polymorphic in java, your methods are polymorphic, so when you access a variable it is statically linked.

Thus your code n.a is statically linked with the a instance variable of _416 class as the reference n is of type _416

So when you call n.addFive(); it actually call the method of Bar (polymorphism). And in this method you add the 5 to the instance variable of Bar class and becomes 13 on the other hand instance variable of _416 remains intact which is 3.

Thus when you execute System.out.println("Value="+n.a); it prints the value of instance variable of _416 which is 3

Edit

Statically linked mean that your compiler resolves your ivar/methods at compile time and dynamically linked means that your compiler defers the linking for run time. That's the brief idea for more you can read this thread on stack

Community
  • 1
  • 1
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • Ahh ok.. Then does it mean that, though method calling decides based on real object created, variables are called based on its reference type? :) what did you mean by statically linked? – user3789200 Aug 06 '14 at 07:16
  • And if I need to print 13. Wht should I do? – user3789200 Aug 06 '14 at 07:23
  • For printing 13 there are several ways. One is to create a new reference `Bar b = (Bar)n;` and then `sop(b.a);`. – Inder Kumar Rathore Aug 06 '14 at 07:29
  • @user3789200 You can also cast n to `Bar` type: `System.out.println("Value=" + ((Bar)n).a);` – Kao Aug 06 '14 at 07:29
  • Oki... Thanks alot for both of u... :) really appreciate your valuable help.. Now I am in a dilemma, thinking that whose answer I must select as the correct 1... :D Can't even vote... :( Btw thanks again for help... – user3789200 Aug 06 '14 at 07:39
  • @user3789200 Hiroshi stack is great site to help each other keep on asking and answering. – Inder Kumar Rathore Aug 06 '14 at 09:31
1

Because both superclass and subclass have a field called a, you've got two fields called a but field from superclass is hidden.

Let's see what happens in your code:

_416 n=new Bar();

Above code creates an instance of Bar class. But, since Bar is a subclass of _416, The constructor of _416 is called, which initializes field a from _416 class. After this, constructor of Bar is called, which initializes field a from Bar. then you 've got actually two fields:

  • n from _416 = 3
  • n from Bar = 8

Then, when this code is executed:

 n.addFive();

It calls overriden method addFive() from Bar class. This method increases value of a from _416 class. So, you've got:

  • n from _416 = 3
  • n from Bar = 13

And, when, you are using field n.a here:

System.out.println("Value="+n.a);

, you are using field from Bar class. That's why printed value is 3.

Kao
  • 7,225
  • 9
  • 41
  • 65