Java: What is the role of protected members?

Discussion in 'OT Technology' started by morgue, Oct 19, 2006.

  1. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    I don't see the difference between protected and public members of a class.
    I know private members can only be accessed by that specific class, but what about public vs protected.
    What are protected members for?
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    private is only accessible within the scope of the class.
    protected is only accessible within the scope of the class and any subclass.
    public is accessible by all.

    protected is like private in that it restricts access to within the class scope, but classes that inherit from a base class are allowed access to the base classes' protected members and cannot touch the base classes' private members.
     
  3. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    So why use protected and not public?

    Define "all" (bold)
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    All, as in any scope inside or outside of the class scope.

    Restricting access to data enforces the intent of your class design. Why design a class interface if there is nothing that enforces its use? You can document your code with when or why a certain data member needs to be changed, or not changed. But you can actually enforce your rules by using information hiding and designing an appropriate class interface.
     
  5. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    This is close but not quite accurate.
    Private members are accessible to any instance of that class, even if they are different instances of the class (in other words different objects).
    Protected members are visible to any member of the package containing the class.
    Public is visible to all.

    When I say visible, I mean that unless they are declared as final, then they can also be manipulated, not just seen.
     
  6. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
    Wrong on both. Skinjob is correct. Private members are available to all instances only if declared as static. Protected members have nothing to do with the containing package.
     
  7. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Ahh my professor was wrong about the private member. I programed this up to "prove" you wrong and got a error. LOL...stupid doctors...wth do they know. :)

    My protected defn is correct tho:
    http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
     
  8. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    No actually I, and my professor, were correct.

    Private members are accessible from other instances of the same class.

    My first test program was incorrect. Here's the code to the one that works....feel free to try it yourself.

    Student.java
    Code:
    public class Student {
       private String name;   
       public Student() {
        name = null; 
       }
       public Student(String n1){
         name = n1;
       }
       public void setName(Student o){
        name = o.name; 
       }
       public String getName() {
        return name; 
       }
    }
    
    test1.java
    Code:
    public class test1 {
      public static void main (String[] argv ) {
        Student a1 = new Student("Stud");
        Student a2 = new Student();
        
        System.out.println ("a2's name before change = " + a2.getName() );
        
          a2.setName(a1);
        
        System.out.println("a2's name after the change = " + a2.getName() );
      }
    }
    
    hahaha owned

    Here's the output:
    a2's name before change = null
    a2's name after the change = Stud
     
    Last edited: Oct 20, 2006
  9. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    god java is awful
     
  10. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    You're accessing (calling) a public member (getName()), not a private one, try declaring public String getName() like private String getName() and you'll see it won't let you... You do can access private members of a class using public/protected members of that same class from another class (like you did), but you can't access its private members directly from another class.

    Hope that clears the confussion :)

    My question now is:
    why does something like this works?

    Code:
    public class B {
        
        protected void protectedPrint() {
            System.out.println("Protected");
        }
        
        public void publicPrint() {
            System.out.println("Public");
        }
    }
    
    Code:
    public class BTest{
    
        public static void main ( String args[] ) {
        
            B b = new B();
            b.publicPrint();
            b.protectedPrint();
        }
    }
    

    I would have thought that line b.protectedPrint() wouldn't work because class BTest is not extending class B.
     
  11. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    You are wrong...getName is NOT where the private member is accessed.

    Look in Student under the setName() method. I'm passing an object to that method...then DIRECTLY CALLING THE PRIVATE METHOD with my line:
    name = o.name;

    This demonstrates that I am directly accessing a private member variable WITHIN ONE INSTANCE OF A CLASS ( the o object ) from another instance of the class ( the a2 object ).

    I'M STILL CORRECT BITCHES!! :rofl::rofl::rofl::rofl::rofl:

    I believe this is because both your class B and your class BTest are created in the same package. Look at the link I provided earlier....classes within the same package can access protected members of classes in the same package.

    Packages are different than classes.
     
    Last edited: Oct 20, 2006
  12. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    I used to think the same thing until I started studying it. In certain ways I like it a LOT more then C++ but it other ways, C++ owns.
     
  13. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    Well you don't have any private mehods.

    And when I say directly I mean something like a1.name on class test1, now the rest of that passing objects part is just confusing to me.

    When you pass the Student object I think it works because you are calling the private variable from inside its class. Still what everone says is correct, you can't access it from outside that class. I don't know about Nocera's comment on static though...

    For what I've understood so far:
    - Private: Access only from that class.
    - Public: Access from any class.
    - Protected: Access from that class and its subclassess.

    However I'd like to see a code for protected like the one I posted to see how to make it unnacesible from another class.
     
  14. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    Yes they do, it's exactly what they are for, for example this wouldn't work because they aren't on the same package:

    Code:
    package test;
    
    public class A {
        protected int protectedX = 1;
        
        protected void printProtectedX() {
            System.out.println(protectedX);
        }
    }
    
    Code:
    package test2;
    
    public class Atest{
       public static void main (String args[]) {
           A a = new A(); //creating instance of class A
           a.printProtectedX();
       }
    }
    
     
  15. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    OMFG...you can't be serious. No wonder you are having trouble.

    You need to stop assuming I'm wrong and try to understand what I'm saying. I am right on this issue even if you don't understand.

    LOOK AT THE FIRST THING IN THE STUDENT CLASS. IT's called
    PRIVATE String name;


    Access modifiers work the same for ALL MEMBERS. That means Member Variables AND Member Methods. SO because name is private, it can't be accessed from outside the CLASS. It's a trivial matter to set up a method to do the same thing. SET IT UP AND CODE IT YOURSELF IF YOU DON"T BELIEVE ME.

    However, if you don't understand what I'm saying by now and that you are still wrong on this issue....you prolly won't code it up properly.

    Here's the idea...I'm making an object of Student type called a1. That object has a PRIVATE member variable called name. I pass a1 to another object (OF THE SAME TYPE AS a1) called a2. Inside of a2, I DIRECTLY call a private member of the a1 object.

    get it??

    I'm not inside a1 calling a private member of a1. I'm inside a2 calling a private AND NON STATIC member of a1.

    Therefore, my original post is correct. Private members (variables and methods) can be accessed from objects of the same class....even if they are different objects.

    My point is different.

    You are not actually inside one object (a2) calling private members of a different object (a1).

    I would say it this way
    Private: Access from inside any instance of the class
    Public: Access from anywhere
    Protected: Access from inside any instance of a class within this package.

    I haven't yet reached the point where we define our own packages. Google Java Packages and try to figure it out yourself. You'd have to set up your class in one package and your tester in another package, then I would bet that you can't access those items.
     
  16. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Oops, I see what you are saying. In my previous post I said I was calling a private method, not a private variable. heh...
     
  17. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    You troll too much lol

    And making things large, using caps and screaming doesn't work...
     
    Last edited: Oct 20, 2006
  18. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    lol...I don't usually troll....must be a full moon or something cuz I'm in a really strange mood tonight.
     
  19. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    Anyway... So if I have object b == object a, that means I can use b.privateMember???

    privateMember beeing a private member of object a
     
  20. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    Can you make another example? A simpler one if possible please
     
  21. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Not quite right.

    Think of this this way. You have to be inside an instance of a class to call a private member of a different instance of the class. Instances of classes are just objects.

    So if you have 2 objects of the same type (call them o1 and o2) then if you are inside either one of these, you can call the private members of the other one directly.

    For example if you are inside o1, you have access to all the private members of o1 AND o2 and any other object of the o1 type. This is why in my first example, I actually pass an object to a method because I can't call it from main.

    So if I was in main, I can't do this:
    Code:
    Object o1 = new Object();
    Object o2 = new Object();
    o2.<private member> = o1.<private member>; //COMPILE ERROR
    
    This errors out because I'm not actually inside o2 when I make the call to the o1 variable.

    If that makes more sense, perhaps you will understand my first example more now.
     
  22. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    I can't think of an easier way to do this right now cuz I'm getting sleepy.

    Perhaps asking a question will help:
    Ask yourself how the name gets changed from null to Stud in a2. Because when a2 is created, it's value is set to null so how could it possibly get changed.

    Does that help?
     
  23. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    Yeah I got it, you are working from inside the same class that has the private member.

    I got protected also, classes have to be on the same package to be able to access one another's protected members.

    And public I believe is, and please correct me if I'm wrong, if i'm on a class that's on a different package from the one I want to access, I would have to import that other class it and then I would have access only to its public members.

    Is that how it goes? holy crap my mind is going to explode :big grin:
     
  24. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    :bigthumb:

    In regards to the public part of your post, if you import a class then all the rules of public/protected/private apply...but I think that's what you ment.

    Anyways, I apologize for my attitude last night....man was I in a mood or what?! :rofl:
     
  25. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    You omitted one piece of info about protected. Subclasses (even if they are in a different package) can access protected items from the parent class.
    For example:
    Code:
    package test;
    
    public class A
    {
        protected int protectedX = 1;
    
        protected void printProtectedX()
        {
            System.out.println(protectedX);
        }
    }
    
    Code:
    package test2;
    
    public class B extends A
    {
        public void updateX(int value)
        {
            protectedX = value;
        }
    
        public static void main(String args[])
        {
            B obj = new B();
            obj.updateX(7);
            obj.printProtectedX();
        }
    }
    
     

Share This Page