No one likes Mondays, so each Monday I like to walk through a complex development topic and give a dead simple explanation of how it works in the real world. So even if your day is rough at least you’ll have learned something new, or at least a new way of thinking about it.
Today I will cover how object oriented programming (also called OOP) works. When I started programming over a decade ago I learned how to build applications procedurally with languages such as C and PHP. If you’re not familiar with procedural code, it simply means that you build the program in sequential order and call methods when you want shared behavior between pages in the application. For example, if I had an invoicing application, I’d have a page for creating a new invoice, another page for showing the invoice, etc. And each page would have scripts that would call methods such as ones connecting it to the database or rendering a date in a specific format.
Now that you have a high level view of how procedural programming works, how does object oriented programming work? I like to think of object oriented programming as a way of modeling a program, where all of the functionality is abstracted so it can be shared throughout the application. Let’s take a look at a real world case study.
Imagine that you’re building an application that allows users to create accounts and you also need to have the ability to make some people administrators. In object oriented programming you’d have a User class, in that class you’d have methods such as:
- Sign in
- Encrypt password
Now we have the issue that you have two types of users: regular users and site administrators. For the most part both user types are pretty similar, the only differences will be things such as editing pages and admin type tasks. In procedural programming you’d need to create two different types of users which would lead to having quite a bit of duplicate code. However in object oriented programming you can simply create a User class, and then create RegularUser and AdminUser classes that inherit from the parent User class.
When child classes inherit from the parent class they get access to all of the methods and functionality of the parent class, and then you as a developer can give them custom behavior. So for our case study the AdminUser class would contain methods unique to it that the RegularUser class wouldn’t have, which would give it the administrator functionality.
Do you see how that works?
Object oriented programming, at at high level, is all about being able to structure code in a way where functionality can be inherited and behavior modified however the developer sees fit. If done properly object oriented programming can lead to very elegantly written programs that have minimal code duplication.
I hope you have a better idea of how object oriented programming works, obviously this was a high level view of how it operates, in the resource section I’ve placed links where you can take a deep dive and learn more about how it works along with how to build real world object oriented programs.