GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Why use protected??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why use protected???" Watch "Why use protected???" New topic
Author

Why use protected???

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Okay.. so protected can be used so other packages can use the methods and variables, but why just use public instead. What does protected provide you over public???
-Dale

------------------
What's this H2SO4 doing in my fridge?? ( thud )


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Even within the same package - if something is protected than other classes don't have access to it. Unless they derive from the class.
(I am new to java) In OO you make something "protected" to ensure that only subclasses have access to it. It you want everyone to have access you use "public". (Packages are new to me.)


Please ignore post, I have no idea what I am talking about.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Are you saying that if you have a class that is not a subclass of the object, that I should not be able to access the method (much like private). I created this class and the method with a protected method called myClassA.addNumbersProtected(4,5) that obviously exists in ClassA. I can still get to it. I'm not sure I understand your description.
----------------
package testpackage;
public class ClassA {
public ClassA() {
super();
}
private int addingMoreNumbers(
int number1,
int number2,
int number3,
int number4)
{
return addNumbers(number1, number2) + addNumbers(number3, number4);
}
int addNumbers(int number1, int number2)
{
return (number1 + number2);
}
protected int addNumbersProtected(int number1, int number2)
{
return (number1 + number2);
}
}
public class ClassB{
public ClassB() {
super();
}
public void addingMoreNumbers(int firstNumber, int secondNumber)
{
ClassA myClassA = new ClassA();
myClassA.addNumbers(4,5);
myClassA.addNumbersProtected(4,5);
}
}
[This message has been edited by Dale DeMott (edited July 11, 2001).]
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi,
youve got 4 access modifiers:
private: only class itself
protected: only class itself and subclasses
package: only classes inside the same package (you do not write any access modifier)
public: everyone
the rule is to make stuff as private as possible.
karl
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Okay.. so as I understand it.. this is how it stands...
private: only class itself
friendly: only class itself and subsclasses inside the same package (you do not write any access modifier) aka friendly
protected: only class itself and subclasses
public: everyone
So if I create a class with friendly methods, classes outside of the package that are subclassed to my base class will not see the friendly methods.
And if I create a class with protected methods, classes outside of the package that are subclassed to my base class WILL see the thest protected methods.
Is this right?
-Dale

------------------
What's this H2SO4 doing in my fridge?? ( thud )
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Classes have access to all non-private stuff of other classes within the same package. (So they don't have to be sub-classes to access the protected stuff of other classes.)
Classes have access to public stuff of classes in other packages.
Classes have access to public & protected stuff of classes in other packages if they are derived from that class.
Hopefully that makes sense.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

To answer your original question assuming that my last post was correct)
You use "protected" to force classes, in different packages, to derive from your class to be able to access protected methods & data members.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why use protected???