[JAVA] Encapsulation, polymorphism
I commented on Separate article, but I will write it as an article.
Encapsulation
** Encapsulation is not the only way to restrict access. ** **
** Anything can be setter / getter to destroy the encapsulation. ** **
In general, "to make" means "to make".
"Article" means "article".
"Japaneseization" means "Japaneseization".
"Encapsulation" is "encapsulation".
- Drug capsules are used as a group of medicinal ingredients, such as "a general cold medicine that is taken 3 times a day after meals to relieve fever, runny nose / stuffy nose, and headache." A name that clarifies the conditions and has a clear effect, effect, and purpose.
- Software capsules are data and operations that are grouped together to clarify usage (API) and restrictions, and to be named with clear responsibilities, uses, and purposes.
- Encapsulation makes it easy to achieve the purpose without worrying about the components (data) and mechanism (processing logic) in the capsule.
- If you change the contents of the capsule without permission, the effect will change, so guard with a capsule. It is the responsibility of the user if the effect, efficacy, or result changes by forcibly changing the contents.
- Although there is no guard (access restriction) function in the language specifications of JavaScript and Python, it is possible to have the user guard by naming rules.
- Encapsulation is not the only purpose of encapsulation.
For example, a "communication capsule" can be used without knowing the communication protocol or header format.
Reference: http://e-words.jp/w/ Encapsulation.html
Polymorphism (polymorphism)
** Subclassing is not the only polymorphism. ** **
When you press the "Play" button, the MP3 player will read the MP3 file and play the music, and the DVD player will rotate the DVD medium to play the video and sound (line and sound effects).
It is polymorphism that the same command behaves differently depending on the object.
It behaves differently, but it is convenient because it can be used with the same operation interface.
It can be used in the same way when a new product is released.
The variety of things that can be used increases.
The user does not have a hard time. Burden reduction.
There is an operation interface on the front of the DVD player and a connection interface on the back.
"Playback" is one of the operation interfaces.
Even different devices can be handled in a unified manner by having a common interface. The benefits of polymorphism.
Purpose
- Reduced burden on the user side, code reduction, labor saving
- Easy to switch the side to be used and improve flexibility
- Breaking dependencies and improving independence by using abstract classes / interfaces
Method
- Give the same name (abstract name) if the purpose is the same
- Define a common interface
Implementation means
- Define a common interface and change the implementation for each object
- Make it a subclass and change the implementation by overriding the method
- Overload the method and change the implementation depending on the argument type even with the same method name