| Author |
public vs protected instance members
|
Faisal syed
Ranch Hand
Joined: Mar 25, 2011
Posts: 30
|
|
Hi,
I realize its a very basic question. Nonetheless,
public = java universe
protected = same package+ subclass
This is what I understand. However, when you can subclass(public, non-final class), then any body can inherit the class and use its members.
Is this not the same as public? Please clear my doubt with an example, as when to use public and when protected.
here is my class:
and this is the consumer of ProtectedDemo
Regards,
Faisal Syed
|
 |
William P O'Sullivan
Ranch Hand
Joined: Mar 28, 2012
Posts: 860
|
|
protectedMethod can only be invoked from within the package.
The public method could be invoked from anywhere else.
e.g:
WP
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5858
|
|
William P O'Sullivan wrote:protectedMethod can only be invoked from within the package.
And also by any subclass, regardless of what package it's in.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5858
|
|
Faisal syed wrote:However, when you can subclass(public, non-final class), then any body can inherit the class and use its members.
Is this not the same as public?
If the purpose of these access levels was for security, then, yes, protected would essentially be the same as public since anybody could get access to the members in question quite easily. But that's not what access levels are for.
They're for design purposes, so that developers that are following the rules and good design principles don't use things in a way they weren't intended to be used. If a developer wants to go out of his way to have a bad design just to get access to protected members, it's not the job of the access levels to prevent that.
|
 |
Sriraman Gouthaman
Greenhorn
Joined: Apr 25, 2012
Posts: 1
|
|
Let me explain when you could use protected
"Zorba" the person who created PizzaMaker want a way to make pizza's with his machine in different flavors.
So he made that process a different routine (function addFlavor()) (Note that our new routine is invoked by makePizza function)
Now any one can subclass PizzaMaker and override addFlavor() to provide there own flavoring.
Now there came another issue, what if some one calls addFlavor directly ? like below
"Zorba" felt this is going to be dangerous, this may even damage the machine. ("Zorba don't want to spend too much on product support") So he decided - no one wont be able to invoke this routine directly. Only sub classes could see this routine to change flavoring. So he made it "protected".
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
Faisal syed wrote:This is what I understand. However, when you can subclass(public, non-final class), then any body can inherit the class and use its members.
Is this not the same as public?
They can inherit and use members which have public or protected access. They can not use members which have private or default access. So, no, it is not the same. Using the protected and default access modifiers, I can put very specific restrictions on how my public class can be used, simply by placing it alone in a package.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5858
|
|
Dennis Deems wrote:
Faisal syed wrote:This is what I understand. However, when you can subclass(public, non-final class), then any body can inherit the class and use its members.
Is this not the same as public?
They can inherit and use members which have public or protected access. They can not use members which have private or default access. So, no, it is not the same. Using the protected and default access modifiers, I can put very specific restrictions on how my public class can be used, simply by placing it alone in a package.
But, going back to the OP's point, if I want access to your protected members, all I have to do is declare a class that extends your class, so, while protected does offer some, er, protection, it is trivial to get around it. (Though this is not a actually a problem or shortcoming, as I pointed out in my previous reply.)
Now, if your class is itself package-private, then nobody can inherit from it, unless the subclass is also in the same package, which does start to touch on security issues, once we get into signed and sealed jars and the like.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4749
|
|
Faisal syed wrote:Please clear my doubt with an example, as when to use public and when protected.
I'd just add that it very much depends on what kind of "instance member" you're talking about.
Most of the good advice you've already been given refers to methods; as for attributes (fields), my advice would be:
1. Never define public fields, unless they're also static and final.
2. (perhaps a bit paranoid, but it's my rule of thumb) Avoid protected fields too, unless they're final.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
 |
|
|
subject: public vs protected instance members
|
|
|