• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

confusion in protected modifier

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a public class Amir in package aaa.It has a protected variable x.
I have a class New1 in package bbb.this class extends Amir .
I have one more class Anand in package aaa (Amir and Anand are in same package)

I thought I cant access variable x from Anand class through the reference of class New1

but when I tried it comiled and gave proper output.

how is it possible?can anyone explain this.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
protected variable x can be access in the same package or subclasses of the class in which they are declared isint it? Since Amir and Anand are in the same package....

New1 has access to x and all instantiations of New1 will provide this value i guess....correct me if im wrong...
[ October 04, 2007: Message edited by: suresh mulagala ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"amir wipro", please check your private messages. You can see them by clicking My Private Messages.
 
amir cdm
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
protected variable x can be accessed in the same package or subclasses of the class in which they are declared isint it?

yes thats correct.

but my question is ,There is no connection between Amir class and Anand class.Im accessing variable x from New1 instance.not from Amir class instance.

see Anand and New1 are in different packages, and Anand not a subclass of New1 class .But Anand can access the inherited protected variable of New1 class.

How is it Possible.


New1 has access to x and all instantiations of New1 will provide this value i guess....correct me if im wrong...
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"amir cdm" is not much better than the first name.

There are not many rules on the ranch, but propper naming is one of them.

I know the answer to your question.
Are you interested?
Change your user name.

Bu.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic