OOP
ООП
Object-oriented programming is a way to organize code around objects that combine data with operations. The rest of the program can use those operations without knowing how the objects work internally.
Also known as: объектно-ориентированное программирование, object-oriented programming
What object-oriented programming means
Think of a playlist in a music app. It remembers which tracks it contains and offers operations to add, remove, or reorder them. The track order is its state; the operations are its behavior. If the playlist checks that a track exists before moving it, the rest of the app doesn't need to repeat that check every time it reorders a track.
In class-based languages such as Java and C#, a class describes the data and methods its instances have. Two playlists can be instances of the same class and support the same operations while holding different tracks. Not all OOP is class-based: JavaScript objects, for example, can share behavior through prototypes.
The ideas behind OOP
- Encapsulation controls how other code interacts with an object's internals. A playlist can expose a
moveTrackmethod without giving callers a mutable reference to its internal list. - Abstraction describes what callers need to know and leaves out irrelevant details. A caller relies on what
moveTrackpromises to do, not on how tracks are stored. - Polymorphism lets the same calling code work with different implementations of an operation. A playback button can call
play()whether the source is a local file or a stream. - Inheritance lets a subclass inherit behavior from a base class and override it where appropriate. It's one tool for sharing behavior, not a requirement for polymorphism.
Example: swapping implementations
This TypeScript example gives two objects the same interface without making them subclasses of anything:
interface Formatter {
format(name: string): string;
}
const plain: Formatter = { format: name => `Hello, ${name}` };
const loud: Formatter = { format: name => `HELLO, ${name.toUpperCase()}` };
function greet(name: string, formatter: Formatter) {
return formatter.format(name);
}
console.log(greet("Sam", loud)); // HELLO, SAM
greet depends on the Formatter contract rather than a particular implementation. Passing plain changes the output without changing greet or adding a branch. The objects don't carry mutable state, and they don't need to: what matters here is that their behavior can be used interchangeably.
When to use it
Objects are useful when rules belong with the state they govern, as with a shopping cart or a UI control. They also help when several implementations serve the same role, as in the formatter example. But a one-off calculation usually doesn't need a class. If one object just needs another object's behavior, consider composition: give it a collaborator instead of putting both in an inheritance hierarchy. For example, a report generator can hold a reference to a formatter without being a kind of formatter.
How it comes up in interviews
You might be asked to add a new implementation without editing its caller, or to explain why a proposed subclass doesn't belong in a hierarchy. Go beyond naming principles: describe the contract, who relies on it, and what would break if an implementation violated it.