[Logo]
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
44 Beta 44
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.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature


This thread has been viewed 139 times.

All times above are in ranch (not your local) time.
The current ranch time is
Apr 27, 2018 00:34:05.