In what situations do we make variables public and methods private?

I am currently learning the basics of Java and C ++. I read in Let Us C ++ that in almost every case, we make private instance variables and methods public for security purposes. But this book also mentions that in some cases we make public variables and private methods.

I constantly think in which cases we will do this. Can someone explain this.

+4
source share
8 answers

Private methods (or private member functions in C ++ terminology) are mostly useful as helper functions. For example, think about what you want to implement fractions, but want to make sure that your fraction is always normalized. Then you can use the private member function normalize() , which normalizes your fraction and which is called after each operation, which can lead to an unnormal fractional part, for example (C ++ code):

 class Fraction { public: Fraction(int num, int den = 1); Fraction operator+=(Fraction const& other); Fraction operator*=(Fraction const& other); // ... private: int numerator, denominator; }; Fraction::Fraction(int num, int den): numerator(num), denominator(den) { normalize(); } Fraction Fraction::operator+=(Fraction const& other) { int new_den = denominator*other.denominator; numerator = numerator*other.denominator + denominator*other.numerator; denominator = new_den; } Fraction Fraction::operator*=(Fraction const& other) { numerator *= other.numerator; denominator *= other.denominator; normalize(); } void Fraction::normalize() { int factor = gcd(numerator, denominator); numerator /= factor; denominator /= factor; } 

Another, special use of private functions in C ++ is based on the fact that in C ++ private only access control is used, not visibility. This allows you to do a simple check before the post-condition in the base class, making the actual function virtual:

 class Base { public: foo frobnicate(some arguments); private: virtual foo do_frobnicate(some arguments) = 0; }; foo Base::frobnicate(some arguments) { check_precondition(arguments); foo result = do_frobnicate(arguments); check_post_condition(foo); return foo; } 

Classes derived from Base override do_frobnicate , while users will call frobnicate , which always checks for pre / postconditions no matter what the derived class does.

+2
source

Usually static final variables are public in the class. If you do not need to change the value of this variable and want other classes to access it, you make it public static final.

Private methods are used only inside the class to perform a task that is internal to this class. As a utility method or some kind of business calculation method. Or simply split the code of the public method into several private methods so that the methods do not get too large.

+1
source

When a method should be used by other methods (public) of the class, and you do not want the object to directly access this method, we make this method as private.

And in some cases, if you want to access your variable directly from a class object, make it public.

0
source

If you do not need it, varibale or methode in other classes do not make it public. This applies to methods and variables.

0
source

private methods are intended for internal use of the class. They can be called from other public classes. This is private because you are encapsulated from the outside world.

for instance

 public void method1(){ method2(); } private void method2(){ // for internal use } 

Public variables are mainly used for class variables, in cases where there is no direct access to variables from outside. for instance

 public static final int FLAG = true; 

You can directly call a variable from the outside.

0
source

It depends on what kind of security you want for each class.

For example, if you have a vector class that has only 3 variables x, y, and z, you should make them public. Many classes are likely to use the Vector class, which is great if they change the values ​​in it.

If you have a Person class that stores a credit card number, background entry, address, etc., you should make them confidential to avoid security issues.

However, if you have all the variables as private, and you provide all accessors and mutators for them, you effectively make them the same as public ones (but with a lot of work).

EDIT: All constant variables must be publicly available, because you still cannot change them. Static variables can be as depending on the situation. It is probably best to have static get and set functions for static variables.

0
source

Private variables or functions can only be used in the class in which they are declared.

Public variables or functions can be used throughout your application.

So, you must declare private all those variables and functions that you are going to use ONLY in the class in which they belong.

Example:

 public class Car { private String model; public setModel(String model) { if (model != null) this.model = model; } public getModel() { return model; } private doSomething() { model = "Ford"; } } 

In the Car class, we declare the String model private, because we will only use it in the Car class, by doing this we assure that other classes cannot change the value of this string without using the setModel function.

The setModel and getModel functions are publicly available, so we can access the private model variable from other classes ONLY using these methods.

In this example, the setModel function checks to see if the value is null, in which case it does not set the value. Here you can see that if you declared the String model publicly available, you would not have control over what value it writes.

The doSomething function is private, and other classes cannot use it. For the other hand, just like this function is private and belongs to the same class as the String model, it can change its value without using the setModel method.

0
source

Rule of thumb, you make public methods when other classes have access to them. internal methods or helper methods must be either protected or private . protected , if you want the method to be extended by those who extend your class, however, if you do not want this to simply mark them private .

0
source

Source: https://habr.com/ru/post/1487417/


All Articles