# Object-oriented programming

> Organizing code as objects that bundle data with the actions that use it.

- Level: Intermediate
- Topics: Software
- URL: https://tidbit.legios.com.ar/object-oriented-programming/
- In Spanish: [Programación orientada a objetos](https://tidbit.legios.com.ar/es/object-oriented-programming/)

**Object-oriented programming**, or OOP, is a way of organizing code around **objects**. An object bundles some data with the functions that work on it. A `Customer` object might hold a name and an email and know how to `sendInvoice()`. A **class** is the template: you write the `Customer` class once and create thousands of customer objects from it.

Three ideas usually come with it. **Encapsulation**: an object hides its insides and shows only what others need, so you can change the internals without breaking the rest. **Inheritance**: a class can extend another, so a `PremiumCustomer` reuses everything in `Customer` and adds a discount. **Polymorphism**: different objects can answer the same request in their own way, so code can call `pay()` without caring whether it is a card or a bank transfer.

OOP grew out of languages like Simula and Smalltalk in the 1960s and 70s and became mainstream with C++ and then Java. Java, C#, Python and Swift all lean on it. It is not the only style: functional programming, which favors plain data and functions without hidden state, has gained ground, and most modern languages mix both. Leaning too hard on inheritance is a classic way to end up with code that is hard to change.

## An analogy

A car factory. The blueprint is the class, each car off the line is an object, and you drive any of them with the same pedals and wheel without knowing how the engine is built.

## Keep in mind

- An object bundles data with the functions that act on it.
- A class is the template; objects are the instances made from it.
- Encapsulation, inheritance and polymorphism are the core ideas.
- Java, C#, Python and Swift are object-oriented; most languages mix styles.

## Go deeper

- [Object-oriented programming (MDN)](https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Advanced_JavaScript_objects/Object-oriented_programming)
- [Object-oriented programming on Wikipedia](https://en.wikipedia.org/wiki/Object-oriented_programming)

## Related terms

- [C and C++](https://tidbit.legios.com.ar/c-and-cpp/): Two old, fast languages that sit underneath operating systems, browsers and games.
- [Java](https://tidbit.legios.com.ar/java/): A long-lived programming language behind many bank systems, big business apps and Android.
- [Python](https://tidbit.legios.com.ar/python/): A popular programming language known for being easy to read and quick to write.
- [Swift](https://tidbit.legios.com.ar/swift/): Apple's programming language for building apps for iPhone, Mac and its other devices.
