What is Object Oriented Programming?

867 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

The idea is that if you use nothing but global functions and variables in a big, complex program, you’re going to have more/worse bugs than if you used a bunch of hermetically-sealed objects each with its own internal functions and variable settings inaccessible to the rest of the program. YMMV.

Anonymous 0 Comments

Here’s a very simple example using animals.

Say you want to write a program that holds information about dogs and what noise they make when they bark. You could just write out a long program like this:

“`
var chihuahua_likes_walks = false;
var german_shepherd_likes_walks = true;

function bark_chihuahia() {
return “yip”;
}

function bark_german_shepherd() {
return “woof”;
}
“`

But what if you want to add another dog? Well you’d need to add `golden_retriever_likes_walks = true;` and `function bark_golden_retriever() { return “woof”; }`, and you’d need to rinse and repeat for every dog breed you wanted to track, and it’d be a bit silly because most dogs “woof” and only a few “yip”

OOP introduces the idea of classes, and inheritance and such. The above code can now be made into:

“`
class dog {
var likes_walks = true; // All dogs like walks by default

function bark() {
return “woof”; // All dogs woof by default
}
}

// This makes a new “dog”, and overwrites some stuff
class chihuahua inherits dog {
likes_walks = false;

// Here, we overwrite the bark function
function bark() {
return “yip”;
}

function hates_you() {
if(math.randomInt(0, 1) == 1 {
return true
} else {
return false
}
}
}

// This makes a new “dog”, but doesn’t overwrite anything
class golden_retriever inherits dog {

}

// …

var gr = new golden_retriever();
var ch = new chihuahua();

gr.bark() // returns “woof”;
gr.hates_you(); // Doesn’t work, as golden_retriever doesn’t have a `hates_you()` function
gr.likes_walks; // is true

ch.bark() // returns “yip”;
ch.hates_you(); // returns true or false because we defined it
ch.likes_walks; // is false
“`

Yes the code is longer, but is much more manageable. Need to add a new breed of dog? Just add `class labrador inherits dog { }`. Want a sub-breed? `class taco_bell_dog inherits chihuahua { function bark() { return “yo quiero taco bell” } }`

I used this fairly extensively when I was writing a game. I had a base class that had a lot of functions and properties shared by EVERYTHING in the game (e.g. the name of the thing, where it was located, a `move(x,y)` function), then I added in more and more specific classes, like “player”, “object”, “npc” and “room”. The `npc` and `player` classes shared lots of the same thing (e.g. `inventory`), and when I added in `cat` as an NPC type, I didn’t need to re-write the `move(x, y)` function because all the code was already in the `base` class

It just made things a billion percent more manageable, because instead of managing 50 functions to do something simple like move a character from `1,1` to `2,1`, I just had one function, and if I needed something special (e.g. a cat moves twice as fast as a person), I just overwrote the `move(x, y)` function for that one special case and everything else just used the `move(x, y)` function defined higher up the chain.

Anonymous 0 Comments

Think about it literally: an object in the world. The kitchen sink, for example.

Your sink probably isn’t an artisanally hand-crafted original, it is based on a plan (i.e. the definition of a ‘class’ or ‘type’ of object). The plan lists all the properties of the sink: it’s height, width and depth; its material (chrome or enamel). 

The plan also lists the functions of the sink. The toggle stopper function allows water to be held in the sink, or sent down the drain. The faucet function takes input from the user to provide either cold or hot water. And there are handles to act as an interface to provide precise input within required constraints.

Your sink is an instance of that plan. It has all the properties and functions of the plan, as does any other object of this specific type. But it can also have properties related to its present state. Full or empty, dirty or clean, functional or broken, etc. In this way, your instance of the sink may differ from another instance of the same sink plan, or from the plan itself.

Anonymous 0 Comments

instead of having
String carName;
String carColor;

Void moveCar() {}

you can have class Car that has name and color and move()
it’s a way of grouping related data and functions together, it’s much more than that but that’s the gist of it.

Anonymous 0 Comments

In Python and JS almost everything is an object, even if you think it’s not. If you only code is Python and JS that’s probably why this is hard to understand, so you may want to try studying a lower level language like C++ to better understand the difference.

An object contains multiple pieces of data together. In JS you might need an integer, so you can write let x = 3. In JS this isn’t actually a primitive integer though, it’s a Number object.

Why is using a Number object instead of just a primitive integer useful? Because it gives you a lot of other features that you don’t have to think about. Like if you added 0.1 to x, it will automatically become a float value of 1.1. In lower level languages, adding 0.1 to an integer x would just result in x not changing, it’s an integer so it can’t store that extra float value. You would have to cast it to a float to get the desired result.

You also get some methods with the object like toString, which are functions built into the object. Every Number has these so it makes it easier to call the object’s method rather than writing your own function for it and passing the integer or float to your function every time.

Yes, you can write programs without using objects at all in lower level languages. For complex projects this just gets harder and more tedious to do. Using objects lets you pack a lot of code into something simple without having to think too hard about it. Abstraction is there to make your life easier.

Anonymous 0 Comments

I think of it as object is a container of some related logic, ie a repository to write/get stuff fr database, an http client or some entity that contains business logic. You can then encapsulate stuff in it and expose publicly only those things that should be accessed from outside. This makes it way easier to understand what an object does.

Polymorphism and inheritance are less used concepts, it adds complexity and should not be used lightly. Would not focus on those when learning.

Anonymous 0 Comments

An object is kind of like an object on real life. 

You have something called a dresser. They all have basicallt the same shape, and they have the same available operations like “open drawer” and “close drawer”. They also have some stuff that varies between dressers. The outer dimensions, the number and size of drawers, the type of drawer pull.  

So in OOP you would make an object called a dresser and each time you needed to work with a dresser in a house you would create a specific instance of “dresser” by defining thr variables for that instance and that instance would have all the operations like “open/close” available to it. 

Then you start creating all the other objects you might need to furnish your place. Bed with size, sheet type, #of pillows. Methods like “lie down, sleep, make the bed” and create as many instances of bed as you need that vary one those. 

Keep building till you’ve got your OOP software home with all the functions andnobject types you need.

Anonymous 0 Comments

Object oriented programming (OOP) is an approach to programming that tries to mirror the way humans perceive the world around us – as things (objects) that have properties and can do things or have things done to them. At the core of it it’s as simple as that.

Using this approach to sort out how code should be grouped allows programmers to build very large and complex systems that can not only compose functions into more complex functions but can also reason about the entities those functions are tied to.

There’s usually more to it than that, but that will depend on what language you use and what philosophy you buy into. C++, Java and C# strongly encourage you to use class-based OOP with polymorphism, inheritance, encapsulation and other shenanigans that you’ll likely learn about in college. JS and Python use classes and objects but you won’t find traditional OOP techniques being used with them frequently. The language is only ever a part of the equation. Want to pull it off in C? Give [Object-oriented Programming with Ansi-C](http://www.freetechbooks.com/object-oriented-programming-with-ansi-c-t551.html) a read.

Anonymous 0 Comments

Imagine you have different toys: cars, dolls, and blocks. Each toy is a different type, but they all have things in common. For example, all cars have wheels, and all dolls have clothes. In OOP, we call each type of toy a **class**, and when you make a toy, it’s called an **object**.

A class is like a blueprint. It tells you what a car or doll can do. Then, when you make a toy from that blueprint, that’s the object. 

You can make lots of toy cars from the car blueprint, and each one can be a different color but still have wheels and doors.

Anonymous 0 Comments

You already know what a function is? An object is just data that has functions that act only on that data.

So you have a jump function? What should jump? The only data that function knows about. If you wanted something else to jump, you would have asked THAT data’s jump function.