Confused with Java Encapsulation Concept

后端 未结 6 1603
抹茶落季
抹茶落季 2021-01-03 06:37

Good day!

I am reading a Java book about encapsulation and it mentioned the getter and setter method.

I\'ve read that to hide the attributes, I must mark my

相关标签:
6条回答
  • 2021-01-03 07:09

    You use setters and getters to make the variables accessible from outside your class. In your example you will have

    public class AddressBookEntry {
    
        private String name;
    
        public void setName(String name) {
           this.name = name;
        }
    
        public String getName() {
           return name;
        }
    }
    

    You will access the name property from a UI class (it isn't good to mix UI and business logic in the same class):

    public class MyPane extends JFrame {
    
      public getAllData() {
         String name = JOptionPane.showInputDialog("Enter Name: ");
         AddressBookEntry entry = new AddressBookEntry();
         entry.setName(name);
         // You can't use entry.name = name
      }
    
    }
    
    0 讨论(0)
  • 2021-01-03 07:09

    I find the answers above already answering your question. But, I see another issue with your code, which is, a security and data integrity issue. You are filling your class member directly out of the user's input w/o any validations on it. For example, you'd probably want to check that the email address has an xyz@abc.com pattern. A malicious user might also provide a very long string in the input causing unexpected errors.

    0 讨论(0)
  • 2021-01-03 07:11

    The idea of private class members (attributes, fields) is to access them directly inside the declaring class (not instance!) only. All other classes will have to use other accessors, for example getter and setter methods. So the way how the AdressBookEntry stores the name, address, telNo and email values, is perfectly hidden inside the class.

    You don't have to use getters or setters, although, sometimes there are good reasons to do so inside the class too. Using getters and setters sometimes makes sense if you do some logging or validating before a value is set.

    0 讨论(0)
  • 2021-01-03 07:22

    Yes and no. The point of encapsulation is that it prevents other classes from needing to know what your class is doing behind the scenes. If you store your name in a String (as you've done here), read/write it from a file, or do something different, the point of encapsulation is that to the user of your class it doesn't matter because all they see is String getName( ) and void setName (String name).

    Since the modification of the data is entirely under the control of your class here, it doesn't break encapsulation. If you did store name to file, then you could potentially do so in getAllInfo without any other user of the class being any the wiser. Since the observable behaviour from the outside of the class still hides what the internals of the class is doing, it's still encapsulated.

    That said, this is a very unconventional approach. Like I describe in the first paragraph, use of accessor methods (getters and setters) is a more idiomatic approach, and easier to understand for someone else using your code. You can do what you do, it doesn't break encapsulation, but it's not what I'd call elegant or typical.

    0 讨论(0)
  • 2021-01-03 07:25

    I agree with all the great answers provided. I would like to add another great benefit about using encapsulation and is the fact that by hiding the data from its clients (just as the examples described above) you can guarantee how to set and get these instance variables making your class easier to maintain and test (i.e if you have many clients manipulating your objects variables you don't need to go class by class writing the new functionality, instead you just modify the getter and setter).

    By making usage only of the objects setters and getters, the details of its implementation can be hidden from the outside world. This is a nice advantage so other objects attempting to interact with your class won't be able to break its functionality. This also increases the likelihood that a system will be implemented effectively.

    0 讨论(0)
  • 2021-01-03 07:30

    This doesn't violate encapsulation, as it doesn't expose the internal data to another class. You may wish to look at Model-View-Controller (MVC) though to help separate out your interface from your data.

    Traditional you create a single getter and setter for each variable as needed like so:

    public String getName() {
      return name;
    }
    
    public void setName(String name) {
      this.name = name;
    }
    

    Don't create getters for variables that won't need to be accessed outside of the class, and don't create setters for variables that won't need to be set outside of the class.

    If following MVC and using the traditional getters and setters like above, you would have the view code elsewhere and call something like

    myAddressBookEntry.setName(JOptionPane.showInputDialog("Enter Name: "));
    
    0 讨论(0)
提交回复
热议问题