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 9 of 9

Thread: What's the importance of declaring a variable as PROTECTED?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Lagos, Nigeria.
    Posts
    1
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post What's the importance of declaring a variable as PROTECTED?

    My name is Samson and i am a beginner aspiring to be a professional java programmer.
    I have a mind bugging question here, What's the importance of declaring a variable as protected?
    since protected class instances or members can be accessed by other classes, whether or not they are in the same package.
    Is it just that my knowledge of it is feeble or what? I could just use the " public " keyword instead....


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What's the importance of declaring a variable as PROTECTED?

    Hi, Samson!. My name is Alex and i am a Java beginner too. As far as i know, controlling access to members of some class can affect the way other classes are related to each other (it can affect constructors, fields, parameters, methods and even an entire object or more). I have to practice more to understand better the use of access modifiers but i really think it all depends of your needs for the program. Please read these links for further information:

    Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    In Java, what's the difference between public, default, protected, and private? - Stack Overflow
    Java Access Modifiers - public, private and protected

    I hope that helps,
    cheers

  3. #3
    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: What's the importance of declaring a variable as PROTECTED?

    Quote Originally Posted by realsoundkid View Post
    since protected class instances or members can be accessed by other classes, whether or not they are in the same package
    That is not correct.
    Private is visible to the class where it is declared.
    Protected is visible to the class where it is declared, any class that extends that class, and any class in the same package as that class.
    Only public is visible outside the package.
    The official word.
    Last edited by jps; November 19th, 2012 at 05:36 PM. Reason: Corrected error

  4. The Following 2 Users Say Thank You to jps For This Useful Post:

    LittleCat (November 20th, 2012), realsoundkid (November 19th, 2012)

  5. #4
    Junior Member
    Join Date
    Sep 2012
    Location
    Bangalore,India
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What's the importance of declaring a variable as PROTECTED?

    In practise when you would like to share a variable or method to few classes but would like to hide it for rest of your code you can use protected variable by bundling similar classes in one package and sharing the variable/method as protected.

  6. The Following User Says Thank You to AshishGupta For This Useful Post:

    realsoundkid (November 19th, 2012)

  7. #5
    Junior Member
    Join Date
    Nov 2012
    Location
    Pune
    Posts
    3
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's the importance of declaring a variable as PROTECTED?

    i will give u an example package1 has class-A containing protected member int p.This p is accessible in package1 as well the other class which extends the class-A. Protected member are like default members with the addition that they are accessible in child class as well which provide the ecapsulation.

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

    Default Re: What's the importance of declaring a variable as PROTECTED?

    Private is visible to the class where it is declared and to any class that extends the class where it is declared.
    You mean private is not visible anywhere else other than the class it was declared in.

  9. #7
    Junior Member
    Join Date
    Nov 2012
    Location
    Pune
    Posts
    3
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's the importance of declaring a variable as PROTECTED?

    yes

  10. #8
    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: What's the importance of declaring a variable as PROTECTED?

    Quote Originally Posted by helloworld922 View Post
    You mean private is not visible anywhere else other than the class it was declared in.
    Yes, that is what I mean, thank you for pointing that out. Hope the OP noticed too...

  11. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's the importance of declaring a variable as PROTECTED?

    Quote Originally Posted by helloworld922 View Post
    You mean private is not visible anywhere else other than the class it was declared in.
    Just to note all inner classes also have access.
    Need Confluence & JIRA hosting? Query Foundry | Need Shared Hosting & Virtual Servers? Cloud Shards
    Ultra fast and reliable USA VPS!

Similar Threads

  1. Why cannot the class be protected?
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: September 20th, 2012, 04:42 PM
  2. Importance of a counter in a linked list
    By georger55 in forum Collections and Generics
    Replies: 1
    Last Post: July 20th, 2012, 06:11 AM
  3. Multithreading and its importance in java
    By jessie143143 in forum The Cafe
    Replies: 0
    Last Post: October 13th, 2011, 02:09 PM
  4. declaring a "final" variable?
    By needleman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2010, 07:55 PM
  5. Importance of interfaces
    By jyothishey in forum Object Oriented Programming
    Replies: 9
    Last Post: March 12th, 2010, 07:11 AM