How OOP helps developers write code faster and easier
Table of Contents
What is OOP?
The central idea of OOP is that all programs written using it consist of objects. Each object is a specific entity with its own data and a set of permitted actions. Let’s say you’re creating a product catalog for an online store. Following OOP principles, you first create objects—product cards. Then, you assign properties to them: name, characteristics, and price. After that, you define actions for the objects: updating data, changing characteristics, and interacting with other objects.
Besides OOP, there are other paradigms. The most common is the functional paradigm, which works with functions rather than objects. If you use the functional paradigm to create a product catalog, you shouldn’t start with cards, but with the functions that populate them. In other words, an object isn’t the starting point, but the result of a function.
The structure of object-oriented programming
1. Object.
2. Class.
A template that can be used to build a programming object. For example, an online store might have a “Product Card” class that describes the general structure of all cards. Specific cards—objects—are then created from this class.
3. Method.
A function within an object or class that allows interaction with it or another part of the code. In the product card example, a method could:
4. Attribute.

Let’s look at what these structural elements of an object-oriented system look like in code, using the Java language as an example.
class Item {
private String name;
private String price;
public Item(String name,
String price){
this.name = name;
this.price = price;
}
public void card() {
System.out.println(this.
name + " стоит " + this.price);
}
}
class Phone extends Item {
public Phone(String price){
super("Телефон",price);
}
}
Here, we’ve taken the general Item class with all its attributes and methods and created a subclass in which we’ve assigned the name attribute. Now, when calling this subclass, we’ll only specify the price—the name will be set automatically.
public class Main {
public static void main(String
args[]) {
Phone1 = new Phone("30000");
}
}
Phone1.card();
Basic principles of object-oriented programming
Encapsulation
All information required for a specific object to function must be stored within that object. If changes need to be made, the methods for doing so must also reside within the object itself—external objects and classes cannot do this. Only public attributes and methods are accessible to external objects.
This principle is the essence of object-oriented programming.
The developer creates:
● A class with certain properties;
● A subclass based on it, which takes the class’s properties and adds its own;
● An object of the subclass, which also copies its properties and adds its own.
Each child element inherits the methods and attributes defined in its parent. It can use them all, discard some, or add new ones. There’s no need to redefine these attributes and methods.
For example, in the product catalog:
- The Product Card class has the following attributes: product type, name, price, manufacturer, as well as the methods “Display card” and “Update price”.
- The “Smartphone” subclass takes all the attributes and methods, writes the word “smartphone” into the “product type” attribute, and adds its own attributes – “Number of SIM cards” and “Battery capacity”.
- The Xiaomi 11 Smartphone object fills all attributes with its values and can use the methods of the Product Card class.
Inheritance is clearly seen in the code example above, where first a class was created, then a subclass, and then an object with shared properties.
Polymorphism
The same method can behave differently depending on the object it’s called on and the data passed to it. For example, the “Delete” method, when called on the cart, will only remove the item from the cart, but when called on the product page, it will delete the product page itself from the catalog.
Advantages and disadvantages of OOP
Popular object-oriented programming languages
Languages themselves cannot be object-oriented. OOP is a paradigm that can be applied to writing code in any language.
Certain languages are more suitable for object-oriented programming because they provide convenient tools for working with classes and objects:
● Java;
●Go;
● Python;
● C++;
● JavaScript;
● C#;
●PHP;
● Ruby;
● Scala;
● Kotlin;
● Swift;
● Dart.
The main thing about OOP
- OOP is a development paradigm, a set of rules and criteria by which code is written. Its essence is that all code consists of objects that interact with each other. Other paradigms exist, such as functional programming.
- There are four main elements in OOP: classes, objects, methods, and attributes.
- The object-oriented approach to programming is built on three main principles: inheritance, encapsulation, and polymorphism.
- Programs created using OOP principles are more structured, easier to read, and scalable. However, they are more difficult to write.
- In the OOP paradigm, it is most convenient to write in Java, C++, C#, JavaScript, and some other languages.
