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

"protected" access specifier

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have created a class "testSpecifier1.java" with access specifier as protected , in package testClasses, I can access this class anywhere in same package.
I have to import package in a class to use content from that package. So if I create another class "testSpecifier2.java" in the same package testClasses, do I need to import this package in the beginning of the coding ? Or can I access content within same package without importing package ? How doea this work with static methods ? As static methods dont need the class to be instantiated. If I dont need to import package to be imported for use in another class within same package, what if there are two static method by the same name in two different classes ? Do I need to specify class name before the static method name in order to specify what static methid I want to use ? If there is unique static method in a class which is unique in package, do I still need to specify the class name in order to use this static method ?

Thanks
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote: If I have created a class "testSpecifier1.java" with access specifier as protected , in package testClasses, I can access this class anywhere in same package.
I have to import package in a class to use content from that package. So if I create another class "testSpecifier2.java" in the same package testClasses, do I need to import this package in the beginning of the coding ? Or can I access content within same package without importing package ?


Please try to implement this by writing a simple code. So that you can get a first hand experience of how things work.

nirjari patel wrote: How doea this work with static methods ? As static methods dont need the class to be instantiated. If I dont need to import package to be imported for use in another class within same package, what if there are two static method by the same name in two different classes ? Do I need to specify class name before the static method name in order to specify what static methid I want to use ? If there is unique static method in a class which is unique in package, do I still need to specify the class name in order to use this static method ?


I think you are talking about Static imports- http://download.oracle.com/javase/1.5.0/docs/guide/language/static-import.html
Always better to avoid Static imports as it leads to confusion and difficult to maintain code.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not talking about static imports.

I am just talking about protected class. If class access specifier is protected and I want to use this class members in another class in same package , do I need to import this package in the cclass or can I just use the methods without importing package ?

Thanks
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:I am not talking about static imports.

I am just talking about protected class. If class access specifier is protected and I want to use this class members in another class in same package , do I need to import this package in the cclass or can I just use the methods without importing package ?

Thanks


Good question. What happened when you tried it out? If you haven't yet, then I would recommend that you write up code which does what you want and try to both compile and run it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic