BUZZBONGO TECH GEEKS

YOUR TECH GUIDES
How OOP helps developers write code faster and easier
We talk about the structure and principles of object-oriented programming (OOP).

What is OOP?

Object-oriented programming (OOP) is an approach to software development, a paradigm based on certain rules and principles. A paradigm is a set of standards adhered to when writing code. If you imagine code as a recipe, a paradigm is the way that recipe is presented in a book. This approach helps standardize the programming process, reduce the likelihood of errors, speed up work, and make code more understandable for other developers.

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.

B3599361 64ed 4470 9106 71afeb44ec80
This is what a program written using the OOP paradigm looks like schematically.

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. 

It’s usually faster to write a function than to create objects and define interactions between them. But if the code volume is large, working with disparate functions is difficult.

The structure of object-oriented programming

In code written using the OOP paradigm, four main elements are distinguished:

1. Object.

A section of code that describes an element with specific characteristics and functions. A product card in an online store catalog is an object. The “Order” button is one, too.

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. 

Classes can inherit from each other. For example, there’s a general class called “Product Card” and nested classes, or subclasses: “Household Appliance Card,” “Laptop Card,” and “Smartphone Card.” Each subclass inherits properties from its parent class, such as the product price, quantity in stock, or manufacturer. At the same time, it has its own properties, such as display size for a “Laptop Card” or the number of SIM cards for a “Smartphone Card.”

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: 

● Fill in the card of a specific object with the required information. 
● Update the quantity of goods in stock by checking the database. 
● Compare two products with each other. 
● Offer to buy similar products.

4. Attribute.

Object characteristics in programming—for example, price, manufacturer, or RAM capacity. Classes specify the existence of such attributes, and objects use methods to populate these attributes with data.
Gemini Generated Image Eiqiyqeiqiyqeiqi
This is how the elements of OOP are related to each other.

Let’s look at what these structural elements of an object-oriented system look like in code, using the Java language as an example. 

A class is a general abstraction that describes the structure of objects. Let’s create an “Item” class for a product that has two parameters, or attributes—name and price. We’ll do this using the class constructor—a method that initializes the object.
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);
    }
}
Here, we also immediately define the card method, which will display the name and price of a specific product. We also define the name and price attributes. Now let’s create a subclass whose name will always be “Phone,” but the price may vary.
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. 

Let’s call the subclass and create an object – a phone with a specific price:
public class Main {  

  public static void main(String 
args[]) { 

    Phone1 = new Phone("30000");

  } 
}        
And now, using the  card method  from the class, we can retrieve information about a specific phone:
Phone1.card();
This line of code will display the information: “The phone costs 30,000.”

Basic principles of object-oriented programming

Object-oriented programming is based on three main principles that make this paradigm easy to use.

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. 

For example, a method for entering data into a product card must be defined in the “Product Card” class, not in the “Cart” or “Product Catalog” class. 
This principle ensures security and prevents data within a class from being corrupted by an outsider. It also helps avoid accidental dependencies, where changing one object breaks something in another.
Inheritance

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:

  1. The Product Card class has the following attributes: product type, name, price, manufacturer, as well as the methods “Display card” and “Update price”.
  2. 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”.
  3. 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. 

It’s the same with objects. You can use their public methods and attributes in other functions and be confident that everything will work fine. 
This OOP principle, like others, ensures that there are no errors when using objects.

Advantages and disadvantages of OOP

Advantages

● The object paradigm makes it easier to write code. It’s convenient to create a class or method once and then use it. There’s no need to rewrite dozens of lines of code. You can use special guidelines for writing OOP code—SOLID. 

● Code is much easier to read. Even in someone else’s code, specific objects and methods are usually immediately visible, and it’s easy to search for them to see what exactly they do. 
● Code is easier to update. Changing a class or method in one place will affect all inherited classes and objects. There’s no need to rewrite each object individually, searching for its exact location in the code. 
● Programmers find it more convenient to work in a team. Different people can be responsible for different objects and still benefit from the work of their colleagues. 
● Code is reusable. A class or object written once can be transferred to other projects. Simply write the “Order Button” object once, and you can then insert it, almost unchanged, into various product catalogs and mobile applications. 
● Design patterns. OOP is the foundation for ready-made solutions for class interactions, allowing you to use a template rather than write code from scratch.

Disadvantages

● Difficulty of learning. OOP is more complex than functional programming. Writing code in this paradigm requires much more knowledge. Therefore, before creating your first working program, you will have to master a lot of information: understand classes and inheritance, learn how to write public and internal functions, and study how objects interact with each other. 

● Cumbersomeness. Where a single function would suffice in functional programming, in OOP, you need to create a class, object, methods, and attributes. For large programs, this is a plus, as the structure will be clear, but for small ones, it can be a waste of time. 
● Low performance. Objects consume more memory than simple functions and variables. Compilation speed also suffers as a result.
It turns out that the main function of object-oriented programming is to make it easier to write large, complex programs that teams of developers work on.

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

  1. 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.
  2. There are four main elements in OOP: classes, objects, methods, and attributes.
  3. The object-oriented approach to programming is built on three main principles: inheritance, encapsulation, and polymorphism.
  4. Programs created using OOP principles are more structured, easier to read, and scalable. However, they are more difficult to write.
  5. In the OOP paradigm, it is most convenient to write in Java, C++, C#, JavaScript, and some other languages.