ENES

Object-oriented programming

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

A Customer class works as a blueprint with a name, an email and a sendInvoice action, and three customer objects are made from it

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.

Keep in mind

Go deeper

Related terms

More in Software