You won’t make it very far into your research on the best tips and tricks to learn programming before you start coming across the term ‘object-oriented programming (OOP)’. If you’re new to the delightful world of having computers do things by giving them instructions made of lots of little steps (what we call ‘programming’), you may have no idea how to interpret this idea.
I was confused about it for a while, and I’m glad I can help others gain clarity as they go about learning an important new skill.
“Everything Is an Object”
OOP is distinguished from other approaches in part by the fact that it treats everything as an object. What this usually means is that OOP projects begin by identifying what the basic objects in the project are and how they will be manipulated. Were you to build software to help an elementary school keep track of its students, for example, you would probably start by building a ‘Student’ class with properties like ‘grade’ and ‘name’. This class would be used to make many different student objects, each with its own state, properties, and actions it can perform.
OOP has a number of basic principles adhered to by users of OOP languages. These include:
- Encapsulation – Object implementations are contained in classes, not accessible by other parts of the code.
- Abstraction – Users should only see what they need to see; otherwise, the code should be hidden.
- Inheritance – One class can inherit from another class, allowing programmers to reuse code. You might make an ‘Animal’ class, then a ‘Dog’ class, for example.
- Polymorphism – Objects can take different forms in different contexts. Some programming languages won’t let you add the numbers 4.0 and 2, for example, because the first is a ‘float’ type and the second is an ‘integer’ type. Languages that allow polymorphism, though, won’t throw a fit.
Other Approaches to Programming
OOP is hardly the only game in town, of course. A popular alternative is functional programming.
Functional programming involves conceiving of programs as a series of function calls, which can be nested or not. Functional programs tend to avoid global variables, which are visible everywhere in the code, as well as shared state or data that can be changed. Projects written in the functional style tend to be compact, easier to test, and less prone to doing unpredictable things.
Neither way is necessarily better. They’re both ways of accomplishing coding tasks.
What Are the Pros and Cons of Object-Oriented Programming?
OOP is often favored in large, complicated projects because of the way it forces programmers to organize their code. For the same reason, it can make it easier for more than one person to work on the same codebase. Once a class has been built, it can be reused or inherited from by other functions, which makes programming in OOP more efficient and scalable.
On the other hand, the OOP paradigm is not without its critics. They point to the fact that OOP programs are harder to reason about than functional ones, both because the program’s state can be changing without you being aware of it and because inheritance is a tricky thing to keep straight.
For all this, though, OOP is a powerful, common approach to software design, and it’s one that you should be familiar with.