What is Object Oriented Programming?

899 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

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.

You are viewing 1 out of 25 answers, click here to view all answers.