[JAVA] About encapsulation


I always read it when I explain it, so I summarize it

What is encapsulation?

** Aggregate variables and methods in a class, hide them, and instantiate them for mass production and use **

――A mechanism to put together, hide, and make a lot ――What is a class? --Instance is an "entity" --What is defined in the class can be used by instantiating --So, it's better to think of classes and instances as a set. --Mechanism for making highly independent parts --One of the three major object-oriented elements --Types and entities correspond to sets and elements in set theory


Conceptual aspect (real world)

** Can be classified **

--For example, "American Shorthair", "Japanese Cat", "Pug", and "Shiba Inu" ――The substance of "American Shorthair" and "Japanese cat" of the type "cat" ――The substance of "pug" and "shiba inu" of the type "dog"

Technical aspect (programming)

** You can pass the necessary information to the entity when instantiating. The instantiated target continues to manage its state. Information to be published is restricted for instantiated targets **

--When instantiated, the constructor is executed --Fields are set when the constructor is executed --Constructor is "what to build" --A field is "attribute information" --For example, a class called "Cat" and an instance called "Cat" --The information required for the "Cat" class to instantiate is the "name" (only this time) --When "Cat" is instantiated, "Cat", which is the information required for the instance, is set in the constructor. --Birth of "Cat" ――Because you continue to be a "cat", you can play "cat" as many times as you like.

Use of "classes" in programming technology


public class Cat {
    String name;
    Cat(String name) {
        this.name = name;

    public void cry() {
        System.out.println("Nya ~");

Use of "instances" in programming technology


public class Main {
    public static void main(String[] args) {
        Cat nekosan = new Cat("Cat");


--Unify the roles --Classes can be treated as types ――The terms used in the explanation differ depending on the viewpoint, context, and person. --Approximately the same --Instantiation --Instantiation --Generate --new

-> new part corresponds to the code

--Approximately the same

-> This time (Java), the "Dog {" part. For PHP, "__constract". "Initialize" for Ruby. For Python, "\ _ \ _ init \ _ \ _". "Init" for Swift

--Approximately the same --Field --Member variables --Instance variables

Related article

Summary of why we make it object-oriented

Book information

Akira Hirasawa (Author) Why make it object-oriented, 2nd edition https://amzn.to/2VSrzwe

Miscellaneous feelings

Encapsulation has multiple elements and perspectives when trying to explain programming technology There are many words to explain, and even if the subject of explanation is the same, the nuances may differ depending on the viewpoint, so it is the most important and difficult feeling.

Recommended Posts

About encapsulation
About encapsulation and inheritance
About =
About method.invoke
About attr_accessor
About Hinemos
About inheritance
About params
About Docker
About Rails 6
About Spring ③
About enum
About polymorphism
About Optional
About hashes
java (encapsulation)
About Dockerfile
Encapsulation review
About this ()
About devise
About Docker
About active_hash
About static
About exceptions
[Java] Encapsulation
About scope
[Maven] About Maven
Encapsulation, polymorphism
About exception handling
About Java interface
[Java] About Java 12 features
About Rails routing
About cyclomatic complexity
About exception handling
About AsyncTask Generics
About array multiplication
[Java] About arrays
About ruby ​​form
[Swift] About generics
About Active Storage
About class inheritance.
About Spring AOP
About Ruby Hashes
About singular methods
About getter setter
About build tools
About MacinCloud introduction
[Note] About nil
About keyword arguments
Chew about API
[About double hash]
About installing Cocoapods
Something about java
Where about java
About HttpServlet () methods
About Java features
About ActiveRecord :: PendingMigrationError
About the method
About standard classes