Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: protected Access Specifier

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default protected Access Specifier

    hi people ,
    thanks for support . but i have Simple Question in Access Specifier
    my question is here:
    Can i change the value of a member declared as protected in the main ??

    class A {
    protected String name;
    }

    class B extends A{

    }

    public class x{
    public static void main (String arg[]){
    B obj = new B ();
    obj.name = "Hello";
    }}

    --if it is possible, then how the member is protected ???
    Last edited by spark; August 4th, 2012 at 03:21 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: protected Access Specifier

    I suggest you read about the protected keyword and see what it means to be protected. While you are there check out the other options. It will all be clear if you read that one page..

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: protected Access Specifier

    No, you can't do that. I suggest you look into using a getter method. You don't need to define one in A so you can't access name publicly with A, but with B you can access name publicly.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: protected Access Specifier

    I think helloworld922 misunderstood. Perhaps I misunderstood. Maybe this will clear it up:
    public class A {
     
    	protected String name;
    	private String surname;
    }
     
     
    public class B extends A {
     
    }
     
     
    public class X {
     
    	public static void main(String args[]) {
    		B object = new B();
     
                    //protected
    		object.name = "Hello";
    		System.out.println(object.name);
     
                    //private
    		//object.surname = "World";  //Will not work!
                    //System.out.println(object.surname);  //Will not work!
    	}
    }
    The object.name will work as I believe you suggested in your first post. Now that is to assume you have the classes in the same package. Read this page.

  5. The Following User Says Thank You to jps For This Useful Post:

    spark (August 4th, 2012)

  6. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: protected Access Specifier

    thanks .. for explanation .
    it means protected members are visible to the main . as long as it is the same package..

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: protected Access Specifier

    Quote Originally Posted by spark View Post
    ...it means protected members are visible to the main...
    Protected would not just be visible to main...
    Protected is visible to the entire class where it is declared. It is visible to the entire class of any class that extends the original class. It is visible to the entire class of any class in the same package the original class is in. Private would be the keyword that would offer the "protection" I think you expected from "protected String name;" in your original post.

  8. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: protected Access Specifier

    thanks man.

  9. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: protected Access Specifier

    edit: My mistake. I originally thought protected disallowed package access. This is incorrect. Edited post to remove offending content.
    Last edited by helloworld922; August 6th, 2012 at 01:06 PM.

  10. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: protected Access Specifier

    Quote Originally Posted by helloworld922 View Post
    Minor correction: protected is not visible to any other class in the same package. It is only visible to that class and classes which extend that class. There is another visibility mechanism which allows this behavior (the 'default' visibility). This behavior is used when no modifier is used.

    package test;
    public class A
    {
        protected String name;
        String address;
    }

    package test;
     
    public class Main
    {
        public static void main(String[] args)
        {
            A a = new A();
            a.address = "123 fake street"; // ok since they're in the same package
            a.name = "Spock"; // compile error
        }
    }
    I disagree completely. Your code compiles as is.
    ...and with an output:
    package test;
     
    public class A
    {
        protected String names;
        String addresses;
    }
    package test;
     
    public class Main
    {
        public static void main(String[] args)
        {
            A a = new A();
            a.addresses = "123 fake street"; // ok since they're in the same package
            a.names = "Spock"; // NOT a compile error
     
            System.out.println(a.addresses + " " + a.names);
        }
    }
    Output:
    123 fake street Spock

  11. #10
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

  12. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: protected Access Specifier

    Ya that is the same link I posted which states:
    The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

  13. #12
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: protected Access Specifier

    Sorry, I didn't see the link.

  14. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: protected Access Specifier

    Quote Originally Posted by jps View Post
    I disagree completely. Your code compiles as is.
    ...and with an output:
    package test;
     
    public class A
    {
        protected String names;
        String addresses;
    }
    package test;
     
    public class Main
    {
        public static void main(String[] args)
        {
            A a = new A();
            a.addresses = "123 fake street"; // ok since they're in the same package
            a.names = "Spock"; // NOT a compile error
     
            System.out.println(a.addresses + " " + a.names);
        }
    }
    Output:
    blah, my bad. You're totally correct. The only excuse I have is a massive brain fart on my part I forget that Java packages behave differently from namespaces and assemblies.

  15. #14
    Junior Member
    Join Date
    Aug 2012
    Location
    kochi
    Posts
    1
    My Mood
    Cold
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: protected Access Specifier

    in the same package it is accessible

  16. #15
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: protected Access Specifier

    thanks all of you .

Similar Threads

  1. How to create a password protected excel sheet....
    By ArunSRPM in forum Member Introductions
    Replies: 1
    Last Post: April 18th, 2012, 01:25 PM
  2. about access modifiers (abstract and protected)
    By Satyanvesh in forum Java Theory & Questions
    Replies: 12
    Last Post: February 2nd, 2012, 12:43 AM
  3. Using Protected Methods, in a Java Library
    By WACman in forum Object Oriented Programming
    Replies: 2
    Last Post: March 10th, 2011, 05:42 PM
  4. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM
  5. Replies: 2
    Last Post: June 13th, 2009, 01:44 AM