CodeGym /Courses /Python SELF EN /Introduction to OOP

Introduction to OOP

Python SELF EN
Level 15 , Lesson 2
Available

2.1 Main Concepts of OOP

The object-oriented programming approach is based on the OOP paradigm. This is when all entities in the program are viewed as objects. Objects are data + methods/functions that perform actions on that data.

You can also say that an object has a state and behavior. The state of an object is provided by its data, which is stored in its internal variables. The behavior of an object is the totality of all actions performed by its methods.

In a medium-sized program, there are many thousands of objects, in a large one — millions. To reduce chaos, it was decided to organize objects into classes, and classes into some sort of hierarchy.

Fun fact! The concept of class hierarchy was taken from the animal kingdom, where there are humans, who are primates, who are mammals, and so on.

Every object has a class through which it is created. On one hand, a class is some kind of template for an object, and on the other — it is an independent entity with its own characteristics (more on that below).

To better understand the concept of classes, check out the following definitions:

Class:

A class is a template or blueprint for creating objects, which defines attributes and methods characteristic of all objects of this type. Classes allow organizing data and functions that work with this data into a cohesive whole.

Object:

An object is an instance of a class. Each object has state (determined by attributes) and behavior (determined by methods).

Encapsulation:

Encapsulation involves hiding the internal implementation of a class and providing an interface for interacting with objects of this class. This helps protect data and control access to it.

Inheritance:

Inheritance allows one class (the child) to inherit attributes and methods of another class (the parent). This facilitates code reuse and simplifies its maintenance.

Polymorphism:

Polymorphism allows using a single interface to work with objects of different classes. This is achieved by method overriding in child classes that are inherited from parent classes.

Abstraction:

Abstraction involves highlighting common characteristics of objects and creating classes that represent these common characteristics. This helps simplify complex systems and improve their comprehensibility.

If you understood at least half of this – awesome. We'll further break down each of these points in more detail.

2.2 Abstraction

A good example of abstraction in real life is job descriptions in a company or organization. The job title is one thing, while the responsibilities of each specific job are something else entirely.

Imagine you're designing the structure of your future company. You might distribute the responsibilities of a secretary among several other positions. You could divide the position of executive director into several independent roles: CFO, CTO, Marketing Director, HR Director. Or, for example, merge the roles of office manager and recruiter into one.

In terms of programming, abstraction is, let's say, the proper division of a program into objects. Usually, any large program can be represented in terms of interacting objects in dozens of ways. Abstraction allows you to pick the main characteristics and omit the secondary ones.

Abstraction is like strategy in warfare. A bad strategy, and even the most brilliant tactics won't save the situation.

2.3 Encapsulation

The goal of encapsulation is to improve the quality of interaction between elements by simplifying their interaction.

And the best way to simplify something is to hide all the complex parts from prying eyes. For example, if you're put in a Boeing cockpit, you won't immediately figure out how to operate it:

On the other hand, for airplane passengers, everything seems simpler: buy a ticket, board the plane, take off and land. You can easily fly from continent to continent, having only the skills to "buy a ticket" and "board the plane". All complexities like preparing the plane for flight, takeoff, landing, and various emergency situations are hidden from us. Not to mention satellite navigation, autopilot, and airport control centers. And this simplifies life for us.

In terms of programming, encapsulation is "hiding the implementation". I like this definition. Our class might contain hundreds of methods and implement very complex behavior in various situations. But we can hide all its methods from prying eyes (wrap their names in "__" on both sides), and for interaction with other classes leave only a couple of methods. Then all other classes in our program will see only three methods in this class and will call only them. All complexities will be hidden within the class, like the pilots' cabin from happy passengers.

2.4 Inheritance

Inheritance has two sides. The programming side and the real-life side. From the programming perspective, inheritance is a special relationship between two classes. But it's much more interesting what inheritance is from a real-life perspective.

If we needed to create something in real life, there are two ways:

  1. create what we need from scratch, spending a lot of time and effort;
  2. create what we need based on something that already exists.

The most optimal strategy looks like this: take an existing good solution, refine it a bit, tailor it to your needs, and use it.

If we trace the history of the emergence of humans, it turns out that since the beginning of life on the planet, billions of years have passed. And if you consider that humans emerged from apes (based on apes), only a couple of million years have passed. Creating from scratch takes longer. Much longer.

In programming, there's also the ability to create one class based on another. The new class becomes a descendant (heir) of the existing one. This is very advantageous when there's a class that contains 80%–90% of the data and methods we need. We simply declare the appropriate class as the parent of our new class, then the new class automatically inherits all the data and methods of the parent class. Convenient, right?

2.5 Polymorphism

Polymorphism is a programming concept. It describes a situation where different implementations are hidden behind one interface. If you try to find real-life analogies, one such analogy would be the process of driving a car.

If a person can drive a truck, they can also drive an ambulance or a sports car. A person can operate a vehicle regardless of what type it is because all have the same control interface: steering wheel, pedals, and gear shift lever. The internal setup of vehicles differs, but they all have the same control interface.

Returning to programming, polymorphism allows for uniform interaction with objects of different classes (usually with a common parent) – a feature that’s hard to overvalue. Its value is even higher, the larger the program is.

OOP are principles. Internal rules. Each of them limits us in some way, offering great advantages in return when the program grows large. The four principles of OOP are like the four legs of a chair. Remove even one, and the whole system becomes unstable.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION