How does Object Oriented Programming work? What does it do and how is it effective?

1.09K views

How does Object Oriented Programming work? What does it do and how is it effective?

In: Technology

10 Answers

Anonymous 0 Comments

The big bois of OOP are Abstraction, Encapsulation, Inheritance, and Polymorphism.

**Abstraction** basically means, when you tell this program that’s a calculator 2^(4) + 3 = ?, you don’t know how it figured out the answer, but you know it did.

**Encapsulation** is how each object has ‘ownership’ of the things inside of it. Basically objects can’t mess with data in other objects unless you explicitly allow for it.

**Inheritance** is just how it sounds, if you have a object inside and object, it can inherit the properties of the parent object. **Polymorphism** is altering the children despite what it can inherit.

For example, your house (parent) has two rooms (children) in it. The air conditioning is at 75 degrees. One room inherits the temperature of the house, 75 degrees. One room as a window unit (a method/function) that allows for it to either inherit the temp from the house (parent) or you can alter it with its own ac (Polymorphism).

Polymorphism is a bit more complicated than that, but for the sake of not having a long winded explanation, that’s basically it.

OOP is sometimes preferred because of its abstraction and encapsulation. It only shows precisely what it has to show, so its a bit more secure, and polymorphism lets you code in a lot of alternate courses of action for the program to take for certain parts of the program.

Anonymous 0 Comments

Instead of step-by-step instructions *to the computer* about what to do, you create objects and instructions for each object for how to calculate things internally, and how to interact (externally) with other objects.

For example, instead of generating this web page from top to bottom:

* Write “Get new reddit, My Subreddits, Home, Popular, All, Random, Users, etc.”

* Write “Reddit, explainlikei’mfive, Comments”

* Write ELI5: How does Object Oriented Programming Work?”

* etc.

You instead:

* Create a Get_Header object, and put code inside about how to make that menu at the top, with all your subscribed reddits.

* Create a Navigation object, and put code inside about figuring out which subreddit the user is viewing (explainlikeimfive) and what tabs (comments).

* Create a Get_Topic object, and put code inside about getting the title of the post, and getting all the replies, and sorting them in order.

* And then you create a Main object, that says, on click, get the user name, and for that user Get_Header(for user), Navigation(for user), Get_Topic(for user), etc.

Advantages are that you don’t have to repeat the steps for every user, you can just use the objects that are there. You can also copy and “inherit the properties” of objects to create more objects. The code is organized better, logically.

Anonymous 0 Comments

The idea is that you can represent things or notions in the world into “classes” then design how these classes relate to other classes. Each class may have data members that represent properties of that object which can be basic programming things like integers and strings or sometimes they may be another class or collection of another class. Then the class specifies functions which may do something useful, perhaps modifying internal data values, creating a new instance of a class, or calculating or returning some value.

Typically object oriented programming involves what programmers called “polymorphism” or the ability to generalize some classes and allow programmers to inherit all the stuff from the general class to describe how their particular version of it works. If used effectively, it allows you to write programs that operate on the higher-level classes, knowing that certain functions and properties are guaranteed to be there, even if you haven’t implemented the concrete instances yet.

It helps in that it allows the programmer to isolate different sections of code (called loose coupling in programming terminology) so that you can make one piece of the application without needing to know everything about all the other pieces of code as well. This is very useful in professional development where you likely have multiple programmers working on different parts at the same time and need to coordinate how everything relates and ties into each other.

Anonymous 0 Comments

The idea is to take the structure of the real-world and use that to define the structure of the software. If I’m making an airplane simulation, my program is divided up like the plane’s parts are divided up. It compartmentalizes change, an airline might switch to an alternative engine, but then I just replace my Rolls Royce Engine object with a new GE Engine object. This sort of change is a lot more likely that replacing all the electrical circuits, so it’s a better modularization choice than having all the electrical modeling in one Electrical object.

It’s most effective when the analogy is strong, like the airplane case above. It’s much less convincing when all the “objects” are made up for the software. It turns out most things we do in software today have been done in reality for decades, so it does work most of the time, but nothing’s perfect in software.

Anonymous 0 Comments

Given that I’m not sure how familiar you are with programming, I’ll base my assumptions on the fact that you’ve heard of Object Oriented Programming (OOP).

TL;DR OOP is good because it chunks related information and actions (variables and functions) together.

If you’ve done any amount of programming, you’re likely aware that sometimes, a set of variables are related. E.g. a “person” might have a name, an age, and a birth date. In functional programming, this could be a struct or just a few loose variables. The thing is, with large applications, passing all of these things around gets cumbersome.

With OOP, you could have a Person class which has all of the relevant data, and even some related functions (aka methods).

OOP also promotes reusability of code. If you have a Person class in one application, all you have to do is copy or import the class to a new project and it should work. Functional programming tends to run into the trap of code being too entangled with each other to be reused easily.

That said, OOP can be used like a functional language (just have 1 class and use only the main method + helper methods), and most functional languages can be made to look a lot like OOP (in C, a struct can have variable and function pointers, and if you split these related variables and functions into their own files, it’s not that different from OOP).

Additionally, OOP tends to make more sense to humans, because in the real world we have objects that do things, and OOP let us basically model that in code.

Does that make sense?

Anonymous 0 Comments

OOP works like Reddit.

The programmer sorts information into subreddits (classes) that each have their own rules. Different users (functions/processes) can subscribe to different subreddits and create the frontpage (outcome) they want to see.

Anonymous 0 Comments

Most basic explanation possible: the “objects” are like little programs you make that are used by the main program to do stuff, for example, if you want to make a program that can do math, one object can be for addition, one for subtraction, ext. and the main program is where you call these objects from to use them.

Anonymous 0 Comments

OOP is one of the most misunderstood topics in programming, but let me try to take a whack at it.

First off: One of the hardest things about programming is managing state — Your program has a bunch of data in it, who’s responsible for effecting changes to each piece of data such that it’s guaranteed to remain correct at all times?

There’s a number of approaches that help with this problem, and OOP is one of them. The core concept of OOP is _Message Passing_ — instead of all parts of a program touching any and all data directly, you have different components of the program that each own some of the data, and components talk to each by passing messages around, asking other components to manipulate data on their behalf. Because each component wholly owns its data, you have only one place in your program (the definition for that component) that has to make sure that it responds to any message it receives by leaving the data in a correct, consistent state.

I specifically avoided the usual OOP vocabulary in that description — in OOP, what I called “components” are usually called Objects, and most languages implement message passing in terms of methods, which are basically just functions that take a special parameter that points at the data owned by the object (usually named `this` or `self`). There’s more than one way to do this, but the most common approach to implementing objects in a language is through classes, which are effectively a template for what data and methods an object has available to it.

Anonymous 0 Comments

It’s all about organization.

At a basic level, there’s only two things that make up a program: data and algorithms. Algorithms are just operations that take some data and produce other data. For example, you sort a list: [4, 2, 3, 1] becomes [1, 2, 3, 4]. Or you get the sum of a list: [1, 2, 3, 4] becomes 10.

Small programs have only a little data and a few algorithms, so organization isn’t a problem. We can just put data wherever and have all of our algorithms in one big namespace, and it might be messy but we can make sense of it. But big programs have a lot of data and algorithms, so in order for us humans to not get confused we have to come up with a system to organize things.

Object-Oriented Programming is a system of organization. We group together pieces of data that go together and put them in classes, and we also put algorithms that only pertain to that data inside the class. Those internals are hidden from the rest of the world, so nobody else has to worry about them, they just interact with the class. So a class might have a bunch of data (called fields) and algorithms (called methods) inside of it, but then all of that gets bundled into one thing.

Then a class can have instances of other classes for its fields. And by grouping things together in a hierarchy like this, we can make sense of very large programs with tons of data and algorithms.

Anonymous 0 Comments

**Please read this entire message**

Your submission has been removed for the following reason(s):

* ELI5 requires that you search the sub for your topic before posting.

There are absolutely no exceptions to this rule.

Users will either find a thread that meets their needs or find that their question might qualify for an exception to rule 7.

Please see this [wiki entry](http://www.reddit.com/r/explainlikeimfive/wiki/how_to_search) for more details (Rule 7).


If you would like this removal reviewed, please read the [detailed rules](https://www.reddit.com/r/explainlikeimfive/wiki/detailed_rules) first. If you still feel the removal should be reviewed, please [message the moderators.](http://www.reddit.com/message/compose?to=%2Fr%2Fexplainlikeimfive&subject=Can%20you%20review%20my%20thread?)