Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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

confused in this error in my code?

 
Ranch Hand
Posts: 649
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


if ii run this code I get output 5. That's ok because method hiding is going on here. But when I delete the void print() of Sup class, I get an error at sb.print(). Someone please tell me why is this error coming even though code has to run print() of Sub class. Then why compiler is checking for print() of Sup class.
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because you use reference of type super class. what method you can invoke determined by type of reference. actual method is method of actual object because overriding
If you delete m() from Super how you will call supRef.m(); at line 9? (this for example one of reason why you get compiler error)

I get output 5. That's ok because method hiding is going on here


and you get 5 because not hiding but because overriding, not hiding.
Hiding is when you use static method in super class and child with same name same signature.
 
Puspender Tanwar
Ranch Hand
Posts: 649
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello @sergej
I gone through many question related to this question and understood that the variable hiding depends on the class type and method overriding depends upon object type. But just look at my another problem



now,if variable hiding depends on class type then why the output of this code is 5.
And if I write int x=5; in Sub(), then output is 2. help me out !!!
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
now,if variable hiding depends on class type then why the output of this code is 5.



There is no variable hiding going on. There is only one instance variable -- that is defined in the Sup class definition.

Puspender Tanwar wrote:
And if I write int x=5; in Sub(), then output is 2. help me out !!!



In this case, you now declared a new local variable named x which you set to 5. This has no effect on setting the instance variable.

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 649
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you henry
but I am not getting the way the code is executing .. can you please elaborate how 2 and 5 are coming as output...
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and your modified code

 
Puspender Tanwar
Ranch Hand
Posts: 649
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you @sergej
understood the concept behind this
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic