• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"protected" access specifier

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic