• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method overriding and variables hidding  RSS feed

 
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()?
 
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!