• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected in subclasses

 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is written in the code below


/////////////////////////////////////////

But after checking i figured out the following:
1- Gamma object IS A Beta, so it can access the protected inherted member x.
2- Beta object IS NOT A Gamma, but Gamma has A Beta, in this case protected members are not visible to the composed object which is Beta object.

please elaborate on this and correct me if I'm wrong

Regards
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look for section 6.6.7 here

and to confuse you , what happened if Alpha's protected x would have been marked as static? remember protected makes sense if it is defined before instance variable/method.
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please remember, protected modifier can have accessible within the package only. If you want to use that variable then leave it as default modifier
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bharath Raja wrote:Please remember, protected modifier can have accessible within the package only. If you want to use that variable then leave it as default modifier


This is not true Bharath Raja , the above code will compile and output:
10
10
0
10
10
which is a proof that your statment is not true
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imad Aydarooos wrote:
Bharath Raja wrote:Please remember, protected modifier can have accessible within the package only. If you want to use that variable then leave it as default modifier


This is not true

oops.. sorry for that... default is only strict package level access
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic