When learning programming languages, I come across the word object-oriented. It is one of the concepts that cannot be avoided because it is used in various programming languages.
It is a fact that many people say, “Object orientation is too difficult to understand,” even though it is a concept that they want to master. This time, I will explain the concept of object orientation, which many people have trouble with.
What is the concept of object orientation?
Many people have heard the name object-oriented, but do not know the concrete way of thinking. First, let’s deepen our understanding of what the object-oriented concept is.
There is no clear definition of object orientation
Object orientation does not have a clear definition. As I mentioned a little at the beginning, object orientation is a concept. That is, there are no hard and fast rules, only loosely defined ones. It is ambiguous and can be interpreted differently depending on the situation.
Therefore, engineers should not think “I want to hold down the object-oriented way of thinking clearly”. ” As an engineer, you should be concerned about the details, but in object orientation, you should not go too far. Since people think, explain, and use slightly different words, we will never have a perfect understanding.
However, if you don’t receive any explanation, your understanding of object-oriented will never progress. In the following, I will briefly explain the concept of object orientation.
Object Orientation for Engineers
For engineers, object orientation is all about “how to develop programs efficiently using objects. ” There are various ways of thinking, but what is ultimately required is to efficiently complete the program.
In general, there are two stages to completing a program: “design” and “implementation”. Let’s understand object orientation from the perspective of “system engineers” and “programmers” who are in charge of each stage.
Object Oriented from a Programmer’s Perspective
From a programmer’s point of view, object orientation is “collecting variables and functions necessary for class objects”. Instead of defining information in pieces, we use object-oriented concepts to define it all together.
If you’ve ever been a programmer, you’ve probably had trouble defining variables and using functions. There are cases where you don’t know where a variable is defined, or when it is called in an unexpected place. Also, there must be some experiences where the method of defining a function is bad and it cannot be called even if you want to call it.
With object orientation, you can prevent such inconsistent definitions with a concept called “class”. As a result, it is possible to make it difficult to cause problems due to uncoordinated definitions.
In object-oriented programming, classes define variables and functions. Classes are reusable and can be created multiple times with the same functionality. Therefore, by using object-oriented programming, it is possible to reduce repetitive descriptions as much as possible.
Object-oriented from the system engineer’s point of view
Object orientation from the system engineer’s point of view is “how to design efficiently”. Object orientation has a concept called class. By using classes, it is possible to define variables and functions in advance and use them repeatedly.
System engineers who design systems need to pay attention to “repeated use”. Object-oriented classes prevent repetitive programming, so we must design them so that they can be used repeatedly.
In addition, creating a versatile design so that it can be used repeatedly is called “generalization.” Conversely, a specific design that cannot be used repeatedly is called a “specialization.” Specialized system design is easy, but generalized system design is difficult, and holds the key to whether or not object orientation can be utilized.
Keywords essential to understanding object orientationIn the above explanation, we have come across unfamiliar terms such as “class” and “object”. From here, I will explain the meaning of the word again.
Class
A class is a blueprint for creating objects that will be explained later . . Define variables and functions as design documents and refer to them when creating objects. It may be easier to understand if it is expressed as a template or model.
In object-oriented programming, variables are called “properties” and functions are called “methods”. There are technical terms, but let’s imagine them in terms of variables and functions here.
Here, to promote understanding of classes, let’s take the “enemy character” that appears in RPGs as an example. There is a possibility that the same “enemy character” may appear in multiple bodies. If you don’t use object orientation, you need to program everything in the same way, such as “enemy character 1”, “enemy character 2”, and “enemy character 3”.
However, if you define an object-oriented class (design document) called “enemy character”, you can easily create “enemy character 1”, “enemy character 2”, and “enemy character 3” with the same ability using this. increase.
Object-oriented programming has various functions that make it easy to use classes. I will explain this later, so let’s first understand that a class is a blueprint role.
Object
An object refers to something created from a class, which is a design document. In object orientation, you don’t work with classes, but with the objects that are created. Creating an object from a class is called instantiation, and sometimes it is called an instance instead of an object.
The class I explained earlier is just a blueprint. So even if you have a blueprint, you can’t do anything. For example, even if you have a blueprint for a house, people cannot live in it, so there is a big difference between a class and an object.
By creating an object from a class, you can register and operate various information. Variables and functions can be used for the first time by becoming an object. For example, if the class “enemy character” explained above has “name” and a variable, you can store a specific value such as “enemy character 1” here.
Also, you can create multiple objects from the same class. As in the previous example, you can create multiple objects from the class “enemy character”, “enemy character 1”, “enemy character 2”, and “enemy character 3”. And you can store a “name” in each of these.
Three major elements to deepen your understanding of object orientationThere are three essential elements to understanding object orientation. I will explain what each element is.
Inheritance
Inheritance is the concept of taking over (inheriting) the functionality of a specific object. It is used to reuse programs and improve development efficiency. Inheritance eliminates the need to write the same content in multiple classes.
For example, let’s consider “brave”, “wizard”, and “pastor” as human characters that appear in RPGs. Let’s assume that all of these characters can be set with “gender”, “role”, “attack power”, and their own “special move”.
Based on object-oriented thinking, it should make sense to create separate classes for “heroes,” “magicians,” and “pastors.” You can also understand creating objects from classes. In the way of thinking so far, you have to define “gender”, “role”, “attack power” and “special move” for “hero”, “magician” and “pastor” respectively.
However, if you use inheritance, you don’t need to define “gender”, “role”, and “attack power” for each. First, since these are common elements, create a class called “human” and define “gender”, “role”, and “attack power” here.
Then use inheritance to define “heroes”, “wizards” and “pastors”. If you specify “Human” as the inheritance source, you can inherit the definition of “Human” without defining “Gender”, “Role”, and “Attack Power” for “Hero”, “Magician”, and “Pastor”. In other words, there is no need to describe the same definition, and it is sufficient to describe only the “special move” part of the “hero”, “magician”, and “pastor” individually.
Encapsulation
Encapsulation is what makes an object’s property information inaccessible from the outside. . To put it simply, it is a mechanism to hide (encapsulate) objects so that they cannot be manipulated by other objects.
Object-oriented programming has various functions, and by using these functions, efficient programming is possible. On the other hand, since it tends to be complicated programming, bugs due to erroneous operations are likely to occur if programming is not done properly.
However, encapsulation can prevent mishandling problems. Since it is in a capsule, it cannot be manipulated arbitrarily, and it becomes possible to prevent the developer from performing unintended operations on the object.
However, encapsulation is not completely inoperable. If the developer defines “how to operate inside the capsule”, it can be operated. For example, you can rewrite the value by preparing a “function to rewrite the value” and calling it.
Encapsulation helps prevent bugs caused by mishandling. It also makes it easier to modify, change, and manage each class and object.
Polymorphism
Polymorphism is translated into Japanese as “diversity”. Calling the same function within an object will result in different execution results depending on the object that is created.
Objects created from the same class have the same variables and the same functions. Since they have the same function, they have the same result. However, this can be inconvenient. That’s where polymorphism comes in handy.
For example, let’s say that the “hero”, “magician”, and “pastor” mentioned earlier have a function called “greeting”. In addition, there was a request that the “hero” should say “Ossu”, the “wizard” should say “Hello”, and the pastor should say “Hello”. For that reason, the only way to think about it so far is to slightly change the contents of the class and create a separate class for each.
However, by using polymorphism, it is possible to change the execution result of the function “greeting”. Even if you call the same function, you can change what is said. I will omit the detailed implementation, but polymorphism is the ability to use the same function as if it were another similar function.
Advantages of using object orientationLet’s also understand the benefits that can be felt by using object orientation.
Facilitate efficient development
Efficient development can be achieved by introducing object orientation. You can write programs without object-oriented concepts, but you should use object thinking in languages that support it.
Object-oriented concepts include classes, inheritance, encapsulation, and polymorphism. Using these eliminates the need for “repeated” implementation and reduces the man-hours required for programming. In other words, efficient development becomes easier.
However, object orientation contains complex concepts. Therefore, until engineers get used to it, it tends to contain bugs due to implementation mistakes and misunderstandings.
Easier to modify and change
Since it is a mechanism that creates objects using classes, it is easy to modify and change classes created from the same class. If you modify the class, you can apply the same modification to all objects.
For example, let’s say that the class “robot” has the functions “run” and “talk”. Let’s consider the case of adding the function of “Reply” to the objects “Robot A” and “Robot B” created from this class.
Adding the function, in this case, is easy, just add a description of “reply” to the class “robot”. The objects “Robot A” and “Robot B” are created from the class “Robot”, so if you modify the class, it can be reflected in the automatically created objects.
If you don’t use object orientation, you have to add “Reply” to the programming part of “Robot A” and “Robot B” respectively. If it is object-oriented, it is necessary to modify multiple things that can be corrected in one place. As mentioned above, if there are only two locations, the benefit is not felt much, but if there are 100 or 200 locations, the difference is clear.
Summary
I also explained the basic idea of object orientation. Object-oriented is the mainstream way of thinking in current programming, so if you are an engineer, be sure to keep it in mind.
It would be nice if you could understand it once, but object orientation is a concept and some parts are difficult to understand. Therefore, it is ideal to deepen your knowledge while touching object-oriented in the field of programming as much as possible.