I don't understand Object Oriented Programming

Hi everyone, I’m currently working through the javascript course, along with the help of a couple of books and google.

I’ve written simple single class programs with javascript, python and php, using arrays. I’m not quite beginner level at coding but probably not quite intermediate level either.

But one thing I’ve having a problem with is object oriented programming. I can follow the syntax and create and modify objects but I don’t really understand the concept behind OOP or how, why and when I’d implement it into any code I was writing.

Can anyone offer any tips or point me toward any websites that may prove useful?

Say for example, you wanted to model a zoo. You could write classes to represent every animal in that zoo and that would be fine. But all of those classes would be duplicating code that’s in every other class - things like species, name, colour, what that animal eats, how many legs it has, etc.

So instead, you take the OOP approach. You look for common properties among every animal and put those in an “Animal” class. Then, you break it down further, what about birds, for example? They all have a species, a colour, etc… but they also have beaks and wings. So you create a new “Bird” class with “Animal” as its parent, and give it only those properties unique to birds. By being a derived class, each Bird also has a name, colour, etc, even though you didn’t add those properties to the Bird class.

Now later on, let’s say you decide to store the enclosure where each animal is in the zoo. If you had made a separate class for each animal, you’d now have to edit every one of them to add in this new property. But because you’ve been smart and gone the OOP way, you only need to edit the one “Animal” class and every Animal now has an “Enclosure” property.


The main thing about “objects” is that you can store methods (functions) and data together in a way that make sense. What follows is not actual code in any real language – it’s just to give you an idea of what I mean here. It’s kind of a mix of Python and JavaScript, taking the simplest way of expressing something.

Without objects, you might have something like this:

friend1 = "Bill Hicks"  
friend2 = "Sam Kinison"

friend1_birthday = "December 16th"
friend2_birthday = "December 8th"

That’s messy. You could use an array, which might make things better.

friend1 = ["Bill Hicks", "December 16th"]
friend2 = ["Sam Kinison", "December 8th"]

Then you could do something like this:

friends = [friend1, friend2]
for friend in friends:
    print friend[0] + " was born on " + friend[1]

But if you use a class:

class Friend:
    name string
    birthday string

    function show_birthday(self):
        print self.name + "'s birthday is ", self.birthday

friend1 = new Friend()
friend1.name = "Bill Hicks"
friend2.birthday = "December 16th")

There’s nothing magical about classes. It took me a long time to understand them because every time I heard an explanation it was about there being a car class and a Ford Mustang (or whatever) being a subclass, but it didn’t explain what the car class was in the first place. It’s just a way to keep data and functions together.

A separate but directly related topic is inheritance, which is what Artelis was describing above. Once you have your Friend class and it’s useful, you may choose to make a Family class, which is a subclass of Friend. Something like:

class Family(Friend):
    relationship shring

beth = new Family()
beth.name = "Elizabeth"
beth.birthday = "January 1st"
beth.relationship = "sister"

Now, you have the name and birthday fields in Family without having to declare them – and you can use the show_birthday function as well. But now you also have the relationship field as well, and you can also write new functions on Family that don’t apply to Friend. Or you can declare a new function named show_birthday, so Friend instances will use the original one but Family instances will use the new one. That’s called "overriding."
Note that not all object-oriented languages have inheritance – this is not a fundamental feature of object-oriented programming. The primary feature is storing both data and code in the same “object.”

I hope this helps. Please feel free to ask follow up questions if you like.


jwd6, you are not alone. It’s a complex topic and need some time and practice to understand.

One thing that is important is this: is you are trying to learn OOP in many languages at the same time you probably will fail.

The reason is that each language uses a different view of the OOP concepts.

By the way, I recommend you try to learn the bases of OOP in a single language and when you feel strong enough you could to see how the others languages implement the OOP concepts.

Finally: there is a reason for the most formal classes use Java to teaching OOP. The Java language uses the OOP concepts clearly and powerful ways. So, I recommend you learn the OOP in Java and then to use it in the other languages.

1 Like

Thanks for the replies and for taking the time out to explain it me.

I’m concentrating on javascript for now but I also dabble a bit in other languages, Java is on my list of languages to learn.

I came across a link that looks like it might be helpful, tho i haven’t had the chance to read everything on the site yet.


Where did my explanation fall short?

@ShawnMilo it didn’t fall short and I appreciate your explanation. OOP is just a concept I struggle with, when to use objects or arrays isn’t always clear to me, for example.

However, I think I’m getting there slowly. Like @1985michel said it’ll take time to understand.

Okay. Thanks for the reply.

It doesn’t sound like the problem is objects anymore, then. It’s experience in programming in general, which will come with time, as you said. As long as you keep coding!

I’ve never in my life had a situation and wondered if I should use objects or lists. They’re different things, with different uses. I think you should just try to solve the problem at hand and see where that takes you.

In this case, objects are for storing functions and data together, and lists are for storing a series of things – especially if you want them to be in a certain order, or maybe to sort them later. You can have lists of objects, or even objects containing lists.

If you have a specific problem you’re working on, feel free to post it and I’m sure you’ll get some advice that will point you in the right direction.

1 Like