Bookmarks

ООП — object-oriented programming

An approach to programming in which a program is built from objects: each one holds its own state and exposes a set of operations on it.

Also: OOP, object-oriented approach

checked 9 Nov

All terms

OOP is one of the main ways to organise a program: data and the functions that work with it live together in objects rather than in separate tables and modules. A large program is easier to split into parts this way: object boundaries set areas of responsibility, and a change inside one object does not affect the others.

Core ideas

An object combines state (fields) and behaviour (methods). Direct access to internal data is closed off: the object is used only through its methods, which is encapsulation. Objects are created from classes, which describe what fields and methods the objects have; a new class can extend an existing one (inheritance) and stand in for it in code that works through a shared type (polymorphism). Instead of asking a neighbouring object for its data and deciding for it, you send the object a request, and that is how objects communicate.

Class and object

pythonA class defines how objects are built; each object is a separate instance with its own state
class Account:    def __init__(self, owner: str) -> None:        self._owner = owner      # hidden field: outside code is not given direct access        self._balance = 0     def deposit(self, amount: int) -> None:        if amount <= 0:            raise ValueError("amount must be positive")        self._balance += amount account = Account("Aziz")account.deposit(100_000)   # the state is touched only through methods
  • Encapsulation: state is hidden inside the object and only methods are exposed, so the object enforces its own rules (an account’s balance never goes negative).
  • Inheritance: a new class takes the fields and methods of an existing one and adds its own, such as “Account” and “Foreign-currency account”.
  • Polymorphism: different objects can receive the same request. You can ask both a “card” and an “account” to “top up”, and the code does not check which type it has.
  • Abstraction: the outside sees what an object can do, not how it is built. A “Report” class can “generate a PDF”; what it uses to do that is an implementation detail.

Languages and where OOP appears

The classic object-oriented languages are Smalltalk, Java and C#, where classes, interfaces and inheritance are built into the language itself. C++ and Python are multi-paradigm: OOP is combined with procedural and functional styles, and part of the code is written as plain functions. JavaScript is based on prototypes, and the class keyword is a convenient layer on top of them. In web development, objects are class instances in Django, React components and document elements in the browser; in the Java and C# standard libraries almost everything is an object.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • SOLIDSOLID principles

    OOP is the approach of building a program from objects, while SOLID is a set of five principles for designing classes within that approach.

  • ORMobject-relational mapping

    An ORM is a bridge between objects in code and database tables: tables are structured differently from objects, and the ORM does not design them itself.

In depth in the references

where the topic is covered in detail