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

Why use protected???

 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic