my dog learned polymorphism*
The moose likes Java in General and the fly likes When I need to use a protected access modifier? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "When I need to use a protected access modifier?" Watch "When I need to use a protected access modifier?" New topic
Author

When I need to use a protected access modifier?

Karim Kiswarday
Ranch Hand

Joined: Oct 01, 2010
Posts: 50

Hi everybody,
my question is concerning about the "protected" access modifier.

I know its functionality, but I don't know when I need to use it.
Conceptually methods in a class could be divided as:
constructors
setters/getters
methods used from clients (i.e other classes)
internal methods (used from other methods in the class)

I can definy classes as public or default (no access modifier). If I set a class as public, in order that other clients (in other packages) can use it I need to set:
PUBLIC CLASS:
constructor -> public
getter/setter -> public
methods used from clients -> public
internal methods -> protected/private (no default because the extended classes, out the package, don't inheritance the methods)

If I set a class as default, only classes in the same package can use it, hence I can use the default safely (public or protected are not useful here):
DEFAULT CLASS:
constructor -> default
getter/setter -> default
methods used from clients -> default
internal methods -> default/private (no protected because I can't extend the class out the package)

OK, after this (terrific) explanation, when I need to use the protected modifier?

I've held the instance variables out because for the encapsulation they need to be private.
When they are private, I can use the keyword super to access at the constructor or at the internal methods (or I can use the getters whether I need to manipulate the data in a different manner).
If instance variables are default or protected, other classes in the same package can modify them.

Is the description above correct?
Where is the mistake?
Do you have examples to show me?
What I need to study to improve my access control concepts (not the rules, the concepts)?

Thanks in advance
KK


No fun at the table, no fun in the sack.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Protected members can be used by classes that extend the enclosing class, even if they're not in the same package. Usually, protected members are defined to allow a subclass to easily add functionality to the base class. These subclasses may need to access members which normally shouldn't be visible to the outside world.

You need to think very long and very hard if you want to make a member protected. Usually, there is no need for them; they are only used in classes that are meant to be extended, and making a class properly extensible is no task to take lightly.

Karim Kiswarday wrote:If I set a class as default, only classes in the same package can use it, hence I can use the default safely (public or protected are not useful here)

This is not true. Even if a class has no modifier (or if it's an internal class and has private access), its public methods can still be accessed by the outside world if it implements a publicly visible interface and these methods are declared in the interface.
Karim Kiswarday
Ranch Hand

Joined: Oct 01, 2010
Posts: 50

Karim Kiswarday wrote:If I set a class as default, only classes in the same package can use it, hence I can use the default safely (public or protected are not useful here)
Stephan van Hulst wrote:This is not true. Even if a class has no modifier (or if it's an internal class and has private access), its public methods can still be accessed by the outside world if it implements a publicly visible interface and these methods are declared in the interface.

I don't understand how it's possible.
If the class (that implement the interface) is not public, I can't access it out of the package.

Can you show me an example?

Thanks,
KK
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16





As you can see, the MyHiddenClass is package private, and it's in a different package than the Test class. However, when we run Test, we still see the line "Everyone can see me!" printed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When I need to use a protected access modifier?