Try to organize your own object orientation

At the beginning

After researching various things in the story of class design, I gradually went back and arrived at object-oriented programming. At that time, I realized that object-oriented programming wasn't really in shape. Since object-oriented is an "idea", can it be defined accurately in the first place? There is a question, but for the time being, I decided to organize it easily in my own way.

Object-oriented principles

There are three principles that come up every time you google object-oriented. ·Encapsulation ・ Inheritance ・ Polymorphism

Encapsulation

Encapsulation is "hiding". What does it mean to hide it? So let's compare it with a car.

There are various parts inside the car. The driver can drive the car without knowing the parts one by one. What the driver should know ・ Step on the accelerator = increase speed ・ Step on the brake = reduce the speed All you have to do is know the "driving method" such as. In other words, you only need to hide the complicated movements inside and know how to use it easily.

The important thing here is that each part is thorough in its role. If the accelerator moves the wiper, or if it is interfered with by the brakes and does not accelerate even if the accelerator is stepped on, it will not be a car.

In terms of programming, it can be said that a "class (part)" has one independent role and should be "easy to use" without understanding its contents. I can store values in a list without knowing what ArrayList.add is implemented.

Inheritance

Inheritance is "standardization / abstraction".

Let's compare it with a car again. There are various types of cars such as light cars, sedans, and trucks. And all cars have something in common, such as "running" and "carrying people." In other words, the standard of car can have common properties.

In terms of programming, it corresponds to the parent-child relationship of an interface or class. Inheritance in java is often used to inherit the methods of the parent class. However, this is just one function, and I think the important thing is the idea of "standardizing / abstracting" the same properties together. A well-standardized source should be clearly visible, with few fixes for feature additions and modifications.

Polymorphism

Polymorphism is called polymorphism, which means that the same method works differently. This is achieved by actually programming what was standardized in the above inheritance.

As compared to the above, there are various types of cars. The manufacturer and type of engine will change depending on the type. However, the driver is unaware of this and can run the various engines on board. That's because it implements the behavior that suits each engine for the same method of "starting the engine".

In other words, Car.startEngine will have polymorphism by implementing various implementations on the inheritance side.

Summary

I think that the idea that is conscious of the above three generation principles leads to object orientation. By being aware of object orientation, you can write highly independent and flexible sources. I think this will make the source easier to modify and more readable.

I was pretty refreshed by organizing while writing in my own way. Of course, I don't think you can immediately consciously program just by understanding the concept. You will be able to acquire it by studying design patterns and accumulating design experience. I will keep trying my best!

I'm going to write it easily for a long time. Thank you!

By the way

Alan Kay, the creator of the word, says that the word object-oriented is a failure. What should be emphasized is not "object" but "messaging" ... [Object Oriented-Wikipedia](https://ja.wikipedia.org/wiki/Object Oriented)

reference

What is object-oriented? I tried to explain with an easy-to-understand example. What I learned after fighting object-oriented programming for 10 years What is object-oriented programming

Recommended Posts

Try to organize your own object orientation
I tried to summarize object orientation in my own way.
Java: Try implementing your own comma-separated formatter
[Must see !!!] I tried to summarize object orientation!
What a beginner did to understand Object Orientation
About object orientation
[Personal memo] I tried to study object orientation lightly
Try to release gem
What is object orientation?
Make your own pomodoro
19 Corresponds to object creation
How to use the getter / setter method (in object orientation)
How to read your own YAML file (*****. Yml) in Java
[Forge] How to register your own Entity and Entity Render in 1.13.2