Using only the tools we’ve discussed so far, you can write functions that manipulate variables in all sorts of ways—the foundation of pretty much any program you want to write. Un­fortunately, as the complexity of a program increases, it be­comes difficult to maintain code that’s organized and easy to understand using only those concepts. As an example, if you were writing code for a bank to keep track of its customers’ accounts, you would quickly end up with hundreds of functions and thousands of variables. It would become very difficult to understand what was going on in the code at any particular place, and more generally how the whole thing works.

That’s what object-oriented programming (OOP) is for. OOP is a paradigm that allows you to group variables and functions together into classes, which are (usually) meant to model things or particular concepts. For instance, in the bank example, we might start by creating a class called “Account,» which simu­lates a user account. Classes are made up of variables and functions (called methods when they’re part of a class), so we start by figuring out what data (variables) and capabilities (methods) an account needs to have. For variables, we might use account number, the account holder’s name, and the bal­ance. For methods, we would want the ability to deposit money, which would increase the balance variable, and withdraw mon­ey, which would decrease it.

Once you’ve defined a class, you have to instantiate it for ev­ery object you’re modeling.

It all sounds very complicated until you get to play around with it yourself, but the basic idea of OOP is that we set up a system of tens, hundreds, or thousands of objects that can cooperate with each other to produce the effect that we want.

Object-oriented programming is not the only programming paradigm in use, but it is the most common. Understanding the core concepts of classes, objects, and methods is the last hurdle to programming in languages like Java, C#, and Python.

Like this post? Please share to your friends: