If we want to express one of the main differences between a machine and a human, it will be that if we speak to a human in his understandable language, and that our speech is not related to his unknown domain, he will understand a very high percentage of our speech. But for a machine, the language it understands is not the language we speak. Computer programming language is the only way we express our wishes to the machine. In this field, we have various examples, such as Procedural Programming, Object Oriented Programming, Functional Programming, and…
Table of Contents
objective programming
Object Oriented Programming, which is also called OOP for short, is a type of logic or pattern that was taken from our daily lives and was introduced around the 1960s. In object-oriented programming, we see the program as similar to the world around us and our daily lives, in fact, based on object modeling.
In the beginning, it was procedural programming, in which the codes were written and used one after the other without any particular order and structure, which certainly brought many problems, for example, imagine writing code in the form of You are behind and want to implement the payment operation. Naturally, wherever the user needs to pay, you must repeat the payment codes. I think you have understood the main problem of procedural programming. With the following example, you will understand more easily the importance of object-oriented programming.
A concrete example of object-oriented programming
To fully understand object-oriented programming, let’s start with an example: there are many appliances in a kitchen; Consider the refrigerator in your home. Characteristics such as weight, feet, color, type of compressor, etc. are among the things that can be considered as the characteristics of a refrigerator. In the continuation of the refrigerator, our example includes a set of behaviors that, for example, start to cool the refrigerator at a certain level. Maybe even in some of the more advanced models, the refrigerator will start making ice with the sensor it has.
Any entity (in our example, a refrigerator) that has a special characteristic, information, status, or behavior is called an object, this object can be considered physical or logical.
The information and features available for an object are called attributes, and the actions that an object can perform are called behavior.
So, in our example, the Object is our refrigerator, and the characteristics of this object, such as the number of feet, whether the freezer attribute is high or low, and finally, at what temperature it starts cooling, can be called Behavior.
What does the concept of class mean in object orientation?
In simple language, the class can be considered as a template for objects, for example, a side-by-side refrigerator is an example of an object of the refrigerator class, classes usually contain the characteristics and behavior of objects, for example, the refrigerator class contains the characteristics of a refrigerator, such as feet, weight, color, type of compressor, etc., and any refrigerator model we consider from this class will have all the features mentioned in the class.
If you look carefully, you will notice that the identifiers and behaviors we defined are not specific to a particular model. Here we try to define a refrigerator in general; We say that the refrigerator of our program has these identifiers and behaviors.
A refrigerator may have other identifiers and behaviors, but these are the identifiers and behaviors required by our work. This will help us to design a map of a refrigerator and then we will use this class to make an object; We make refrigerator items in detail.
For example, using a Refrigerator class, we can create different refrigerator objects that have different models, engine types, and years of manufacture, but all of their behaviors are the same.
In this way, object-oriented programming allows you to easily model the complex behavior of a real system. With object-oriented programming, data and functions (identifiers and methods) are combined into one object.
This issue reduces the need to have shared data or global data in the program. This is the main difference between object-oriented programming and procedural approaches.
Principles of object-oriented programming
Object-oriented programming is based on the following four cases:
1) Encapsulation
The properties of each object are placed in the scope and class of the object itself. Objects no longer have the power to access and modify class data and can only access a list of publicly defined class functions. This feature in object-oriented programming has increased security and prevented unwanted corruption of information.
2) abstraction
With the property of abstraction, in object-oriented programming, no code is written in the main body, and for this reason, we cannot directly create objects from abstract classes. The abstract concept, because it does not have specific code, helps programmers to easily make changes and additions over time. For example, if we have a print method in a program, we cannot define the body of this method. Then create child classes from this method define body codes in these child classes and easily create objects from them.
3) Inheritance
In the concept of inheritance, we can implement unique objects and relationships for an object. If these features are duplicated in another object, it can easily inherit common features from its higher class without damaging the unique properties of both. come in This feature in object-oriented programming provides a more accurate data analysis to the programmer reduces the development time and increases the level of accuracy. For example: if we consider a human being as an object, the father of the family may have colored hair and colored eyes.
4) Polymorphism
Using the concept of polymorphism, we can easily define a public method that has different inputs each time. For example, if we consider humans as objects, they all eat, then we can consider a method called FOOD for them, but we must be careful that not all people eat the same foods. Therefore, the FOOD method can be used every time and for each person with different inputs.
These four principles are object-oriented programming principles that are used in this programming model. Therefore, it can be said that with object-oriented programming, coding becomes very flexible and organized. For this reason, programmers prefer this model to the old models. In the following article, we will only name a few programming languages that are based on the principles and concepts of object orientation and use the concept of object orientation.
Advantages of object-oriented programming
- Increase application security
- Reduce maintenance cost
- Reusability
- Easier program analysis
- Ability to organize codes more optimally
- No need to write duplicate codes and features that have already been implemented and save resources
- The ability to divide the program into smaller but independent programs