Protected usage to share variable in package - java

Is protected attribute a valid method (best pratice) to use (read and modify) variables in various classes of the same package, or is there any motivation to don't do this?

If your class isn't mean to be extended, you can also try the default modifier.
Protected allows access from all classes in the same package and from all classes that subclass your class. Default allows access only from inside the package.
Check this for more information on access modifiers.
You should also consider using getters and setters.

This is one of the main intentions of protected. The other one is for usage in sub-classes. So yes, this is a good way of doing it. But ... I would use protected (getter/setter) methods instead, if possible. Don't have the variables themselves as protected, and don't modify them directly.

Related

Java abstract class - Should the instance variables be private or protected?

Should the instance variables be private or protected in java abstract class?
Simple question. I am trying to get more insight into the concept of abstraction in java.
Thanks!
As a rule of thumb, go for non-final private variables. If your design calls for giving derived classes access to these variables, provide protected methods for accessing them.
Using protected variables creates maintenance liability in all classes, abstract or not. As soon as someone inherits from your abstract class, your protected variables become exposed as if they were public. Here are some reasons why this variables should be avoided:
Inheriting classes can change your variables at will - this may go around variable validations set up by the abstract base class
Inheriting classes become dependent on variable names and types - this locks in the design choice that you made when defining protected variables.
First rule does not apply to final variables because they cannot be changed, so the rule makes an exception for them. Second rule still applies, though, so you should be careful about defining protected variables, even in situations when they are final.
If protected then this class and any subclasses may access the property. If private then only this class may access the property (it is not inherited). It depends on if you need to access them in any subclass.

Access modifier which is essentially private, but can be accessed by children

In Java, is there a way to have an access modifier which is essentially private, except that this method can be accessed by children? I am developing a program in which there are several methods which are needed by the children of a certain class (and want to avoid duplication), but I don't want to make it public as I do not want the objects these childish classes are instantiated in to have access to these methods.
If there is no such thing, what would you guys recommend as to achieving best practice on this issue?
Preferably in the same package - protected doesn't fulfil this.
This is what the protected keyword is for.
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
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.
Edit: Please be also aware that the Java Reflection API allows any class to access any member of your classes whatever the modifier.

Protecting internal class variable in jar

We have a getter method within a class.
Within the same JAR we want the variable to be accessible with the no-identifier access level, from the same package and subpackages.
Below the access levels from: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
Our problem is, how do we stop someone from taking the compiled .JAR, creating a class with the same package namespace definition, and being able to access our variable through the getter?
We thought about getting rid of all getters for the specific variable, and giving the variable value to other classes with setters and constructors, when they pass a reference to themselves. Obviously, they will be final classes. This way all objects needing the variable value have their own private copy.
I'm wondering though if there is a better way?
Access control modifiers (public, private, protected) are not meant as a security tool, but as an OO design tool. They're used to implement OO patterns like encapsulation, inheritance.
Even with no getter whatsoever and a private variable, any Java developer can use reflection to access the variable.
If you want to keep something secret, don't ever put it in a variable of a program executed by anyone. Keep it on your own machines.
- Reflection seems to be the evil here, using which any variable even with private access modifier can be accessed.
- Four access controls like private, default, protected, and public are introduced in Java more as a tool to support the Core Object Oriented Concept like Inheritance, Encapsulation etc...

Private and public java access modifier basics

I am relatively new to java and I did some reading about private and public acccess modifiers. I would like to eliminate any confusion once and forall in this topic because I feel like I don't have the best grasp on it.
Access modifiers in variables
Please correct me if I am wrong, a variable is public on default. If the access modifier is set to public or just not set at all, than other classes from the same project can access the integer and/or modify it. If it is private than it is not visible to the outside and cannot be accessed by classes outside the one in which it was created.
Accesss modifiers in classes
Access modifiers in classes, I don't seem to fully understand. As far as my understanding, if I call a private method from method that is not in the same class, than it will not work. If it is public than it will?
Is there anything I am missing or don't understand correctly?
I appreciate help in this regard.
Please correct me if I am wrong, a variable is public on default.
You're wrong. Assuming you mean fields, by default, they have "package" access, which can't be expressed explicitly. Local variables have no concept of access control - they only exist within the context of a method anyway, so can't be referred to from anywhere else.
If the access modifier is set to public or just not set at all, than other classes from the same project can access the integer and/or modify it.
If it's set public, then any code can access it.
If it's default (package) access, then any code in the same package can access it.
Access modifiers in classes, I don't seem to fully understand. As far as my understanding, if I call a private method from method that is not in the same class, than it will not work. If it is public than it will?
That's pretty much right, yes.
I suggest you read the Java tutorial on all of this, and consult the language spec section 6.6 for more details.
Here's a good doc on the subject.
There are four access levels:
private: Only that class (not even descendants) can access.
protected: Only that class and its descendants and classes in the same package can access.
package-private (no specifier)--only classes in the same package can access--even subclasses that are not in the same package cannot.
public: Everything can access.
For for both classes member variables and methods, the default access is package private.
You can find information about the 4 modifiers here on Oracles website.
If you do not specify the modifier, it is said to be on default, which means only any code in the same package can access it.
If it's set public, then any code in any package can access it.
Actually in Java there are four different access modifiers, private, public, protected and package specific. "Please correct me if I am wrong, a variable is public on default" - you are wrong here a variable if not declared differently has a package related access. If it is set to public then it can be modified by other classes even outside of the original package. This website - http://javapapers.com/core-java/access-modifiers-in-java-explain/ - can provide you useful hints. Actually it is very easy once you read a bit more on OO concepts such as Encapsulation. Then you will understand the purpose of access modifiers (to ensure data integerity).
Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:
Default : Visible to the package. No modifiers are needed.
Private : Visible to the class only.
Public : Visible to the world. All classes and packages.
Protected : Visible to the package and all subclasses.
If no access modifier is specified by the programmer, default access modifier is used.
Access Modifiers:
Public - {Can access anywhere in the project}
Private - {Can access only inside the class}
Protected - {Can access within the package and sub classes}
Default - {can access within the package}
Non-Access Modifiers:
Static - {for creating class variable and method}
Final - {for creating finalized class, variable and method}
Abstract - {for creating abstract method and class}
Synchronized - {for threads}
To learn more follow this link
private and public java access specifiers.in java,private access specifiers it specifies the access.private is like own family property that can uses only which family belongs.that means inside the class,method and variable can be used.
public is like government properties that means every one can access the properties without any need of tokens
private and public java access specifiers.in java,private access specifiers it specifies the access.
private is like own family property that can uses only which family
belongs.that means inside the class,method and variable can be used
.
public is like government properties that means every one can access
the properties without any need of tokens

how to declare a method in parent abstract class so that it can be accessed by children?

In Java, I have an abstract class with a method implemented (not abstract) which I want to be accessible by other classes which extend it, but not by everyone else in the package..
I think this is not possible, because private keeps it private, public and protected expose it too much..
How would you approach the problem? Reimplementing the same method in all the extended classes is out of discussion.. is there a way to access the parent methods?
thanks!
If you want the method to be accessible by anything that extends the class then protected is the modifier you want. This is what it is for.
but not by everyone else in the package.. That's not possible. You can use protected access to allow access for subclasses but that will also include access for every class in the same package.
Use protected and make a package that contains only the abstract class and it's subclasses. Don't put any other classes in that package that shouldn't have access to protected methods.
If your concern is that someone else might create a new class in that package and start accessing protected methods, there really isn't a language construct to prevent that. You have either trust your other developers or have a stringent peer review process.

Categories