Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why it displays wrongly?

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why it displays "in sub and 5"
why not "in sub and 10"
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why it displays "in sub and 5"
why not "in sub and 10"


Because polymorphism does not apply to variables.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does display "In Sub10", and then "5" on the next line, which is as it should be. Where did you see it displaying "in sub and 5"?
 
Sureshkumar Chinnappan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output is

In Sub10
5
 
Rohit Suman
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi the answer to your question is that while accessing the fields the
reference of is of superclass type so it access the filed of superclass and prints 5 but when you are calling the method the object is of subclass so the method of subclass is called and since there is implicit this reference so the field of subclass is being accessed and is printing 10
hope it clears your doubt
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fields are invoked depending on reference whereas mehtods are invoked depending on objects.

hope you got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic