Programming is heterogeneous — there are principles and approaches that allow you to write programs in different ways depending on the context and the task. One of these approaches is object-oriented programming. The OOP paradigm is structure, principles, and advantages.
What is OOP
Object-oriented programming, or OOP, is one of the development paradigms. A paradigm is a set of rules and criteria that developers follow when writing code. If we imagine that the code is a recipe for a dish, then the paradigm is how the recipe is designed in a cookbook. The paradigm helps to standardize the writing of code. This reduces the risk of errors, speeds up development and makes the code more readable for other programmers.
The essence of the concept of object-oriented programming is that all programs written using this paradigm consist of objects. Each object is a specific entity with its own data and a set of available actions.
For example, you need to write a product catalog for an online store. Guided by the principles of OOP, first of all you need to create objects: product cards. Then fill in these cards with data: product name, properties, price. And then prescribe the available actions for objects: update, change, interaction.
The schematic representation of an OOP-written program looks like this
In addition to OOP, there are other paradigms. Most common is the functional one, in which they work not with objects, but with functions. If you use a functional paradigm to make a product catalog, then you need to start not with cards, but with the functions that fill in these cards. That is, the object will not be the starting point, but the result of the function.
It is usually faster to write a function than to create objects and prescribe the interaction between them. But if the amount of code is large, it is difficult to work with disparate functions.
The structure of object-oriented programming
In the code written according to the OOP paradigm, there are four main elements:
- Object.
A part of the code that describes an element with specific characteristics and functions. The product card in the online store catalog is an object. The “order” button is the same.
- Class.
A template on the basis of which you can build an object in programming. For example, an online store may have a “Product Card” class that describes the general structure of all cards. And specific object cards are already being created from it.
Classes can inherit from each other. For example, there is a general class “Product Card” and nested classes, or subclasses: “Home appliance card”, “Laptop card”, “Smartphone card”. The subclass takes properties from the parent class, for example, the price of the product, the number of pieces in stock or the manufacturer. At the same time, it has its own properties, for example, the diagonal of the display for the “Laptop Card” or the number of SIM cards for the “Smartphone Card”.
- Method.
A function inside an object or class that allows you to interact with it or another part of the code. In the example with product cards, the method can:
- Fill in the card of a specific object with the necessary information.
- Update the quantity of goods in stock by checking with the database.
- Compare two products with each other.
- Offer to buy similar products.
- Attribute.
The characteristics of an object in programming — for example, price, manufacturer or amount of RAM. The class prescribes that there are such attributes, and in objects, using methods, these attributes are filled with data.
Basic principles of object-oriented programming
Object-oriented programming is based on three basic principles that ensure the usability of this paradigm.
Encapsulation
All the information that is needed for the operation of a particular object should be stored inside this object. If you need to make changes, the methods for this should also lie in the object itself — extraneous objects and classes cannot do this. Only public attributes and methods are available for external objects.
For example, the method for entering data into the product card must necessarily be registered in the “Product Card” class.
This principle ensures security and does not allow damaging data inside a class from the outside. It also helps to avoid accidental dependencies when something breaks in another object due to a change.
Inheritance
This principle is the whole essence of object—oriented programming.
The developer creates:
- A class with certain properties;
- A subclass based on it, which takes the properties of the class and adds its own;
- A subclass object that also copies its properties and adds its own.
Each child element inherits the methods and attributes prescribed in the parent. He can use them all, discard some, or add new ones. At the same time, you do not need to re-register these attributes and methods.
For example, in the product catalog:
The “Product Card” class has the attributes product type, name, price, manufacturer, as well as the “Display card” and “Update Price” methods.
The “Smartphone” subclass takes all attributes and methods, writes the word “smartphone” in the “product type” attribute, plus adds its own attributes — “Number of SIM cards” and “Battery capacity”.
The “Xiaomi 11 Smartphone” object fills in all attributes with its values and can use methods of the “Product Card” class.
Inheritance is clearly visible in the code example above, when first a class was created, then a subclass, and then an object with common properties.
Polymorphism
The same method can work differently depending on the object where it is called and the data that was passed to it. For example, the “Delete” method, when called in the basket, will delete the product only from the basket, and when called in the product card, it will delete the card itself from the catalog.
It’s the same with objects. You can use their public methods and attributes in other functions and be sure that everything will work fine.
This OOP principle, like others, ensures that there are no errors when using objects.