• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Override question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I was expecting 4 is printed first, but how come Beta 44 is printed first?
I think my question is more about System.out.print().
 
Ranch Hand
Posts: 58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With polymorphism, overriden methods and variables do not behave the same way. Methods will be invoked based on the object reffered by the reference. And variabels will be used based on the reference type.
So with your example the output should be =>

4 beta 44
44 beta 44
 
Ranch Hand
Posts: 67
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here in SOP it called the override method... so the answer should be something like that....


Beta 44
4 44
Beta 44
44 44

as Laren said... the variabels will be used based on the reference type. so in first SOP the b.h = 4 ...
 
Wu Wen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Gareta wrote:Here in SOP it called the override method... so the answer should be something like that....


Beta 44
4 44
Beta 44
44 44

as Laren said... the variabels will be used based on the reference type. so in first SOP the b.h = 4 ...



This is the right answer, I understood the polymorphism part, but I don't know why Beta 44 is printed before 4. Please advice, thank you
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is because before execute system.out, the java execute the expressions in the parameter passed for system.out. in other words, in System.out.println(b.h + " " + b.getH()); , first is executed b.getH() and b.getH() has System.out.println("Beta " + h );
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Missed the SOP in getH functions.

1. In any statement if there is an internal call to a method that method gets executed frist.
For example: If theres a call to a method A() in main(), A() needs to be completed first before main() reaches termination.
Likely, A(B()); In this case, B() needs to be executed first before A() returns.

 
Wu Wen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Larsen Raja wrote:Missed the SOP in getH functions.

1. In any statement if there is an internal call to a method that method gets executed frist.
For example: If theres a call to a method A() in main(), A() needs to be completed first before main() reaches termination.
Likely, A(B()); In this case, B() needs to be executed first before A() returns.



Thank you, now I get it
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic