File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why use protected???

 
Dale DeMott
Ranch Hand
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Dale DeMott
Ranch Hand
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 388
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic