Picture a mobile wallet app, the kind half of Dhaka uses to pay for a CNG ride. Rina has 500 taka in it. She pays a 120-taka fare and her balance shows 380. Then she opens the "offers" screen, and her balance suddenly reads 50. Nobody stole anything. A programmer, writing the offers screen, meant to set the cashback to 50 and typed the name of the wrong variable. The balance was a global variable, so every function in the program could write to it, and nothing stood in the way.
Until now, a function and its data lived apart: the data sat in variables, and any function that could see a variable could change it. Object-oriented programming (OOP) answers the worry by sealing the data and the functions that work on it into one unit, an object, and by deciding who may touch what. A wallet object lets you pay and lets you read the balance, but nothing outside it can simply write a new number into it.
By the end of this chapter you'll be able to say why OOP exists and write the POP-versus-OOP table from memory. You'll tell a class from an object, and a reference from the object it points to, in Java, C++ and Python. You'll know when a constructor runs and when a destructor runs, and why a constructor has no return type. You'll hide data behind private and guard it with getters and setters, and you'll read an access-modifier table in three languages. You'll build classes on top of other classes with inheritance, name its five shapes, predict the order in which constructors run, and explain the diamond problem. And you'll separate abstraction from encapsulation, the pair that exam papers love to mix up.
We'll go in five parts, from easy to hard: why objects exist and what class and object mean; how an object is born and how it ends; encapsulation; inheritance; and abstraction. Every program in this chapter was compiled and run, and the output you see is the real output.