[JAVA] Encapsulation review

What is Encapsulation?

Recently, my understanding of encapsulation, which is one of the basic concepts of OOP, has faded, so I will write it for my own review with a very narrow review range.

What is encapsulation? If asked by her who has never programmed Simply put, ** Like a medicine capsule, protect those fine particles inside from the outside. You might say ** first and then explain in code.

~~ I understand that it is an access control to an object from the outside. (Actually there may be more ..) ~~ I received advice in the comments and realized that my understanding was wrong.

** Access control is a means of encapsulation, so it's different from encapsulation. ** **

** Encapsulation description **

Encapsulation is a "class for what" by combining related data and the processing that requires that data into one when dividing software, and excluding irrelevant and irrelevant ones from the class. This is done to clarify the purpose of the class "Is it?", And aims at a state where there is no duplicate data or processing in other classes.

To explain concretely with insanely simple code

Person.java


public class Person {
    private int age;
    private String name;
    //Credit card PIN
    private int creditCardPinNum;
    public double weight = 69;

    public Person(int age, String name, int creditCardPinNum) {
        this.age = age;
        this.name = name;
        this.creditCardPinNum = creditCardPinNum;
    }
    // getter
    public String getName() {
        return name;
    }
    // setter
    public void changeName(String name) {
        this.name = name;
    }

    //Getter that can read age from the outside
    public int getAge() {
        return age;
    }
}

Note: It's my own interpretation to revenge the concept.

First of all, people can't change their age, right? A 10 year old cannot be "20 years old from tomorrow", Therefore, the age of the Person class is access controlled so that it cannot be changed (member variables are made private and the setter method is removed). I have a getter because I can tell others my age (** I don't lie this time **).

Although it is name, the name of a person can be changed any number of times, so the setter method (changeName) is written in the Person class.

Summary

~~ What is encapsulation? External access control (information hiding) ~~ Encapsulation is a "class for what" by combining related data and the processing that requires that data into one when dividing software, and excluding irrelevant and irrelevant ones from the class. This is done to clarify the purpose of the class "Is it?", And aims at a state where there is no duplicate data or processing in other classes.

Recommended Posts

Encapsulation review
Encapsulation
Rails Review 1
Ruby Review 2
Java review
java (encapsulation)
About encapsulation
[Java] Encapsulation
Encapsulation, polymorphism
Ruby Review 1
Rails Review 2
Review about bundler
Enum review notes
Code review points
Java IO review