What is Object Oriented Programming?

895 viewsOtherTechnology

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

25 Answers

Anonymous 0 Comments

In C, you can make a struct, but you can’t make a function belong to the struct.

In C++, you can make a function that belongs to a class. You can make instances of that class call specific functions automatically upon construction or deconstruction. This lets you restrict what a developer is allowed to do or not do with your code. Less to think about and get wrong that way.

Anonymous 0 Comments

Object Oriented Programming is quite self explanatory. You’re able to create “objects” which share methods and constructors, but are still separate entities. It’s easiest to think of it in terms of a video game. You’re able to create a base `render` object which does nothing but output a mesh with textures to the screen, then a new object that builds directly upon the basic `render` but also has ways of simulating physics. Build upon the `physicsRender` object for a character, which takes inputs, reacts to them and can simulate physics once certain criteria are met. On top of the `physicsRenderCharacter` you have `physicsRenderCharacterPlayer`. etc

Or another example would be a program where you classify railroad cars. You start with a basic `railCar` type which specifies how many wheels each car has, the car’s weight, number, and owner. All `railCars` will have those variables set, and from there if you go for a `freightCar` you specify what kinds of loads it has, the volume of freight it can handle. All different types of rail vehicles will fall under the `railCar` type and inherit the variables from it.

No, not all programming languages are object oriented. An easy example is BrainFuck, where you’re controlling individual bits of memory. While it would be *technically* possible to achieve something *similar* to OOP, it’s not an inherent part of the language. Your objects aren’t going to have methods or anything as you know it, and you can’t easily create and manipulate them at will.

Anonymous 0 Comments

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.

Anonymous 0 Comments

At its simplest, I like the old “recipe” metaphor. An object is a cake. A class is a recipe for a cake. If you need a cake, you use the class to instantiate an object. If you need 2 cakes, you use the class again. The two cakes are separate and unrelated to each other, but you know exactly what’s in them and how they taste.

That is, you know the methods they have, etc. You can interact with any number of cakes, because you know what “cake” is capable of, by examining the class (recipe).

There are many other aspects of OO programming, some of which are controversial (ask an old-school c++ coder if you can call something object-oriented without “inheritance”) but that’s the key: you define a class, and then you know how to work with any object made from that class. Very useful as your program grows in scope and scale.

Anonymous 0 Comments

Here’s a 3rd year CS students explanation, one of the main reasons to use object oriented programming is to define objects with states which can be a mix of mutable and non mutable. For example say you’re writing a simple program that defines a car in GTA 5. There are many types of cars each could be using the same model but that doesn’t mean they’re the same object. You would probably create variables that define color, speed, amount of doors, durability, and then even more specific things like whether the tires have been popped, whether windows are broken(so you can fire out of them).

Now I’ll decide not to overwhelm you but I must mention classes to some extent because they’re also interesting. A class essentially stores a blueprint for any kind of object, for example you could create a class that is used for all vehicles in the game, and then you would have sub classes for planes, cars, boats, bikes.

Quickly you’ll realize that making objects allows for intuitive code reuse and general approach for organization. With OOP you’re basically nesting a lot of boiler plate in a generic class so you can focus on the real details instead of having to define a vehicle everytime.

Say Rockstar wants to add a flying boat, they could simply inherent attributes from the vehicle super class, flying vehicles, and boats. Tada you don’t have to define a very simple flying boat because all of the code is already there you’ve just created a brand new object using a scary word called polymorphism(no longer so scary).

It’s incredibly useful but has its drawbacks, python also uses OOP so feel free to explore the paradigm.

Anonymous 0 Comments

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.

Anonymous 0 Comments

The coolest example is [the observer model](https://en.wikipedia.org/wiki/Observer_pattern#:~:text=In%20software%20design%20and%20engineering,calling%20one%20of%20their%20methods).

If you want to have several threads (subroutines of a program running simultaneously) interacting with a variable in functional programming you would have to keep checking if it changed, since the variable can’t really communicate with the rest of the program.

In OOP you can make the variable into an object class. It can have various properties based on functions that said class contains. You can make things public to the outside, or hide them.

For example, one of the tools in OOP is the function that initiates the whole thing. Within that function you can check if another instance of this class exists, and if it does, you order it to commit sepukku. This way there is only one instance of this class within the whole program, even if you forget about it, or share the code with someone else. (This is called a Singleton)

The variable access is locked to the outside (at least that’s the general consensus), so you can’t just write ThisIsINT = 4; you need to politely ask the class to do it for you. So you add a function that changes that variable and then you call the class function (if it’s public) by writing ClassName.ChangeMyINTPlease(4);

Here’s the fun part. Even if you use the class only to store that one value, nobody can stop you from making it announce to the world it has changed. Within that function you can call a different one that alerts other classes that the value has changed. You can even add a popup window with “I’m 4 now!” if you want to.

So the whole observer model relies on classes calling a function that saves the ID of those classes within the observed class, as a list for example, and then you can alert them when things happen. It can be a singleton, but it doesn’t have to be. If you want to have a function that returns the value of that variable multiplied by 3.47, you can do that.

All of it is saved as a class, and whenever you create an object of that class you can expect it to always behave the same way. And if you use a class written by someone else, you receive a box with bunch of holes in it, the author then tells you “if you want the box to do some cool stuff, then throw an apple into this exact hole, if you use any other, it will explode”

Anonymous 0 Comments

You absolutely can just use functions and variables. However as your program gets larger and more complicated, things tend to get messier.

Say you are programming a car in some sort of driving game. You could start with a string for the car’s name, an integer for its speed etc. Now you want to add another car. You could do it again and now have variables named things like car_1_speed and car_2_speed. Obviously though it’s not scalable to do this for hundreds of cars though.

So you can turn to something like embedded lists or a dictionary data structure. This cuts down hugely on how much repetition is in your code. And it is perfectly feasible to have a list with each item a car that is itself a nested dictionary of all of its variables.

However now you start writing functions. You need to have car_accelerate which edits a car’s speed. Maybe car_horn that plays a certain sound given the car_horn_type it’s passed. Where are you putting them? Just top level of your document will work, but then you also need to create a nested dictionary for drivers and all the driver functions. And race courses and race course functions. You can use comments and spacing and even separated files to try and keep things organized, but using objects lets you group things together.

The car object will have variables called attributes as well as functions called methods wrapped up nice and neat inside of it. Now instead of calling car_accelerate and passing it the external info on the car, you can just do my_car.accelerate(). It’s a nice, clean, organized, object that contains what it needs.

Anonymous 0 Comments

It honestly takes a while to wrap your mind around.

The first thing I’ll note is the notion of an object in JavaScript is so different from any other popular language, I think it’s a bad starting point for how to understand traditional class-based OOP. Like most things involving JavaScript IMO, it’s best to just completely ignore JavaScript’s way of doing something as the “norm”, it will make you more confused as to how it relates to the wider world of programming.

IMO the best way is to do it, is understanding them relative to C structs because that’s where modern classes and objects evolved from in most languages. There is at least one good comment here that identified the connection.

The original idea from Alan Kay was to be able to treat bundles of data like structs as tangible objects. Like, you could define a blueprint for a bird or something in code, called a class. The bird would define its own behaviours or interactions. It would be able to tell the world around it that it’s capable of flying, and how.

You could turn this into a template called a class and create many separate and distinct bird objects that could do all the behaviours of a bird. And maybe you then want to that bird blueprint, and be able to make something more specific, like a duck. So you extend the bird blueprint with duck behaviours. It still has the flying behaviours that most birds have, but then you start adding other behaviours like swimming and floating.

In that respect, you can just take the struct in a languge like C, and start turning them into the idea of being a template for objects you can create called a class. This special struct is just a collection of data that can define its own behaviours and be responsible for its own data. Because it’s responsible for its own data, you can tell it to protect sensitive data. Because it acts like a template, you can use that template to extend it and make other similar templates of data and behaviours.

In his original conception, Alan Kay also had this idea that objects would interact by passing messages to keep them from being tightly coupled. That never quite worked out in reality, because of the way modern OOP evolved from C and I guess Bjorn Stroustrup never got the memo or something.

But yeah, basically OOP is just C structs with extra bonus features.

Now, the above describes class-based OOP.

There was also another interpretation of OOP called prototype-based OOP. I don’t know much about the history or context of that and outside of JavaScript.

Anonymous 0 Comments

[https://en.wikipedia.org/wiki/Smalltalk](https://en.wikipedia.org/wiki/Smalltalk)

That’s the best language to use for learning object oriented programming, because it’s a simple and pure OO language.

The reason for OO programming is to make programming easier, safer, faster and more reliable.

They use 4 principles to make things easier, and you’ll have to do some reading to understand them completely, but here’s a basic definition:

Encapsulation hide the implementation details of objects from the outside world (like variable names)

Inheritance allow subclasses to use code from superclasses)

Polymorphism allowing objects of different classes to perform actions with the same name using different code

Abstraction each object only reveals a specific mechanism for usage. Therefore, the code inside becomes largely independent of other objects

[https://career.softserveinc.com/en-us/stories/what-is-object-oriented-programming-oop-explaining-four-major-principles](https://career.softserveinc.com/en-us/stories/what-is-object-oriented-programming-oop-explaining-four-major-principles)