Forums Register Login
Method overriding and variables hidding
Hi there!

So I have the following code:

First of all I'm not sure if my question is actually about method overriding because I'm not sure what causes this. I understand why the result is(I mean how the overriding and hiding works) :

Beta 44
4 44
Beta 44
44 44

What I don't understand is why is not:
4 Beta 44
44 Beta 44

Operations in a System.out.println(), are done from left to right in this case, no? So "4" should have been first because it was hidden; then the method from Beta class comes next, because is was overridden: Beta 44; then the println from inside the method; and last the return from the method: 44, right? For the next System.out.println() is the same process only that h is 44....

Why is "System.out.println("Beta " + h)" from the b.getH() read first, then "b.h", then the return "44" from  b.getH() and last the println from the original System.out.println()?
You are calling the methods from inside the () for the println method calls on lines 16 and 18. That means the called methods will be evaluated first. Since those methods also call System.out.println() on lines 4 and 11, those effectively become "nested" calls. The inner nested calls to println() are executed before the outer ones.
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Why should you try IntelliJ IDEA ?

This thread has been viewed 154 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 20, 2018 12:40:59.