Doubts about encapsulation and objects in OOP

1

One of the main characteristics of object-oriented programming is encapsulation, I wonder, should all the properties be set to private? Even the objects? Is there a case in which this can be dispensed with or is it a rule as such in POO?

Example

package PureShit;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class clase2 {
private File f;



public clase2(String ruta) {
    f = new File(ruta);
}



public File getF() {
    return f;
}



public void setF(File f) {
    this.f = f;
}


}
    
asked by J.newbie 10.11.2018 в 18:00
source

2 answers

1

Within the topic of OOP, we must distinguish the following:

ENCAPSULATION

  • Public = > All the methods and properties that are declared in this way are accessible from any point, that is, inside and outside the class that defines them.
  • Private = > All the elements that are declared in this way are only accessible within the scope of the class that delcaró
  • Protected = > All the elements that are declared in this way are accessible within the class that declared them, as well as only those classes that inherit from the one that contains them.
  • The issue of applying encapsulation must be preceded by understanding the level of access that each method and / or property that you need to declare must have

    EXAMPLE WITH PROTECTED

    In this fragment of code I am indicating that both the property that will store the connection, and the method that will allow me to connect to the database manager, then any class that does not inherit from Server will not be able to access to the aforementioned

    public class Server
    {
        protected String conexion;
    
        protected Server(String server)
        {
            server = server;
        }
    
        public static void main(String args[])
        {
        }
    }
    

    EXAMPLE WITH PUBLIC

    Within the following code fragment, both the connection property and the same connection method are accessible from any point; which for example supposes a problem because not all the classes of your software are going to occupy it

    public class Server
    {
        public String conexion;
    
        public Server(String server)
        {
            server = server;
        }
    
        public static void main(String args[])
        {
        }
    }
    
        
    answered by 10.11.2018 / 18:35
    source
    0

    Everything depends on the architecture of your project and the way people will use your code. The golden rule is to put everything in private until you or another party needs access to that property. Once you need access, you must consider the integrity of the property, if the property has all the objects of the same data type as domain , then you set the property to public . Now if the domain is restricted (the values it can take) you must validate that the property is valid at all times, in this case you must create setters . There are cases in which you want to access the property and make changes that are not directly reflected, in this case you usually create a getter that returns a copy of the property of the otherwise a getter is usually created that simply returns the property (the changes will be directly reflected).

        
    answered by 10.11.2018 в 18:40