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....
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
Re: What's the importance of declaring a variable as PROTECTED?
Quote:
Originally Posted by
realsoundkid
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.
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.
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.
Re: What's the importance of declaring a variable as PROTECTED?
Quote:
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.
Re: What's the importance of declaring a variable as PROTECTED?
Re: What's the importance of declaring a variable as PROTECTED?
Quote:
Originally Posted by
helloworld922
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...
Re: What's the importance of declaring a variable as PROTECTED?
Quote:
Originally Posted by
helloworld922
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.