• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Compile time error

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was just come across one confusion. Below please see the code:



In above code i am getting compile time because in main() method on line System.out.println(a.i); it is not able to get the value of i.

Thanks,
Ram
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any private member (field, method, nested classes) is only accessible from code inside the very same class.

Now class B also has a field "i" that has default (package) visibility, so that should be visible. The thing is, unlike methods, fields are not polymorphic - it uses the reference type (in this case A), not the actual type (in this case B).
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Chhabra wrote:Hi,

I was just come across one confusion. Below please see the code:



In above code i am getting compile time because in main() method on line System.out.println(a.i); it is not able to get the value of i.

Thanks,
Ram



I was able to get it to compile by typecasting a to class B as follows...

System.out.println(((B) a).i);

I think this is more likely the answer you want than simply removing the private modifier on i in class A, which also compiles.

Maybe you need to brush up on your polymorphism

OK Rob got in ahead of me, in light of his comments on Polymorphism, I'm not sure how my answer applies.
 
Rob Spoor
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your cast to B works, because then it is using the i field in class B - which is visible. It's still not using the i field in class A, but that's impossible with the private modifier.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Your cast to B works, because then it is using the i field in class B - which is visible. It's still not using the i field in class A, but that's impossible with the private modifier.



Duly noted. In light of that, I'm not sure what you meant earlier by fields not being polymorphic, but I'll do the research and find out.

It is worth noting that simply making the variable public in A gives the result

--- Class B ---
0

which I'm pretty sure is not what was intended.

best regards.
 
Rob Spoor
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:Duly noted. In light of that, I'm not sure what you meant earlier by fields not being polymorphic, but I'll do the research and find out.


What I meant is that for fields, the reference type (A) is what determines from which class the field is actually accessed. With methods on the other hand, it's the actual runtime type (B) that determines from which class the method is actually accessed.
 
Ram Chhabra
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody..... That clarify my doubt.
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic