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
Very broadly speaking, object oriented programming is the notion that a single variable can carry a lot of complex information… but more important than that, it can carry its own code. “Objects” are these variables and while they include a bunch of fields, they usually also contain functions (aka “methods”) which vary depending on the exact type.
For example, you can have a Human type, and sub-types Man and Woman. They’re similar, and a Man can be used generally as if it were a Human, but exactly what code gets run when you run its functions is unknown to the code that calls it. However the object knows which function to use and it’ll be taken care of.
This gives us the main advantage: so-called “polymorphism”. One variable is many types – its real type, as well as the sub-types it was built upon – “Man”, but also “Human” and whatever other types are down the chain.. Mammal? Animal? It may be used as if it were any of these sub-types by any code that expects one of those sub-types while still behaving as intended. Languages really built for object oriented programming (eg: Python, Java) often have a master type for all objects so that any object can be handled generically, and maybe provide some functions on them to be replaced or improved upon.
Latest Answers