I can’t really wrap my head around OOP. Like I understand an array and a function and a variable, but I don’t understand what an object is. Why can’t we just use functions and variables everytime we need information or code to run? I code in Python and JS mainly, so objects are truly the most important thing, but I don’t really get it. Also, isn’t every program object orientated? Even C, does it not fit the definitions? If so, how is it any different from C++?
In: Technology
You know variables, which are named stores of some value of some type. It’s easy to store numbers, strings, booleans, and so on.
But what if I want to store the information about an orange—it’s color, size, age, and so on? There’s no good way to store all of those things without using several variables and really just remembering that they’re all connected to your orange. In addition, you get huge scope problems when you have to make a bunch of variables to describe a single thing that then exist throughout the lifetime of the code. This can make code messy, hard to work with, and ultimately inefficient.
Objects are the answer to this problem—it’s a data structure that languages let you create that, instead of naming variables, allows you to name instances of an object you define, which in turn is automatically connected to the attributes you tell it to track—size, weight, and so on. This means that, instead of requiring many lines of code to store all those values, I can use a single line of code to create the Orange and give it all those values, then easily retrieve those values since they’re attached to the Orange. In addition, you can create LOTS of oranges, each with their own attributes that are tied to their own specific instances of the Orange object.
Now this is all well and good—but even base C and Rust have these things (they’re known as “structs”), and they’re not OOP languages. OOP languages take this a step further by encoding rich functionality supporting objects that can be described in four points:
1. Abstraction—objects allow the user a high level, simplified view of what can be a complex system of variables and relationships (in the pandas module in Python, I can just work with the DataFrame object in an easy way, when data frames themselves are massive, complex systems).
2. Encapsulation—objects only allow the user access to what the programmer says, keeping unnecessary information and methods away from users who might not understand them, while still allowing the end user to use the objects easily.
3. Polymorphism—objects can behave differently depending on what kinds of objects they are, which makes them more flexible than strict typing languages would normally allow.
4. Inheritance—object types can actually belong to a larger group of objects. In our earlier example, the Orange object can also be part of the larger Fruit object group, where Oranges “inherit” all the traits and attributes of Fruit, but can have specific attributes only for oranges.
OOP uses these concepts to make code more efficient, readable, organized, and ultimately more understandable to both the programmer and the user. It isn’t the only paradigm—there’s another concept called functional programming that solves these problems by making everything related to functions—but it is definitely an incredibly popular paradigm for a lot of modern coding languages.
Latest Answers