Any good explanation for OOP

Hi there,
I have been looking for a read or guide that can make me understand Object Orientated Programming. I have read maybe 25 different explanations, and I still don’t understand this whole instancing stuff. Why not just declare the objects with the properties instead. Any good guide for a dummy out there …
tia.

Have you read any of these yet? I literally Google’d just “object oriented programming for dummies” to find these btw—not to imply that you’re a dummy, but I just figured that the results might give an explanation that you can understand, so no offense intended. :wink:


http://teknadesigns.com/what-is-object-oriented-programming/

I still don’t understand this whole instancing stuff

Instantiating has to do with classes, which is often but not always related to OOP. For example, Javascript does not have classes but is an OOP language.

In regards to instances, think of a class as a blueprint of something you want to make, and an instance of the class is something made from that blueprint. For example, a class could be a blueprint of a building and the actual building would be an instance of the blueprint.

I recommend reading the first half of this chapter from YDKJS - this and Object prototypes if you want to understand class theory better.

edit: spelling

Not to nitpick, but the word is instantiating, not instancing, as using the proper terminology is important. I don’t know what “instancing” is supposed to mean.

An object is an instance of a class, and the process of creating said object is called instantiating it, or instantiating the class.

1 Like

OOP you get an object or concept (most of the time irl) and then try to make that into something similar but in code. An object in real life can have many properties and things it can do, so you grab the ones you need and then create a blueprint for it. e.g. a person blueprint can have hair color, eye color, race, and other characteristics. Then from this blueprint you build several of these ‘objects’. e.g. Susan has brown hair, brown eyes, is white, etc. Something Park has black hair, dark brown eyes, is asian, etc.

The whole ‘grabbing what you need from that concept into the blueprint’ is called abstraction.
The blueprint most languages call those classes.
The characteristics they call them properties, the whole thing the call it it’s state.
The things it can do are called methods. Things like smiling, waving, walking, etc in that previous example.
Each object created from the blueprint is called an instance.

So in OOP you create several of these and work the code like objects. Inside each of those of the properties you can have another object too, just like in real life. An engine is a object but it’s made up of many other smaller ones, the smaller ones are made of even smaller parts, and so on.

Thanks for finding that mistake.

An object is an instance of a class, and the process of creating said object is called instantiating it, or instantiating the class.

That’s not the case in Javascript; I figure that’s worth nothing on this forum since Javascript is FCC’s language of choice.

Indeed, JavaScript is the exception to the rule.

So to qualify that statement, an object is an instance of a class in a programming language that supports classes—not JavaScript (because it’s weird, for lack of a better word), but others like C++, Java, and Python.

And object-oriented programming invariably refers to those languages (C++, Java, Python, etc). While JavaScript does have the concept of objects, it’s not the same concept as established & implemented by the other languages, and the formal terminology in OOP doesn’t apply to JavaScript in the same way, or sometimes doesn’t apply at all (like multiple inheritance).

1 Like

Check out Udacity’s course on JavaScript OOP

Not the easiest thing to understand on the first try, but definitely a good place to learn OOP in Javascript.

Of course to add to the confusion, ES6 does have classes… but they are just syntactic sugar around prototypes.

@soeradjIT :
The main thrust of OOP is combining data and behavior together.

In a non-OOP way of programming, you might create an object:

var person = {"name": "Peter", "job": "Actor"}

and a function that prints that object:

function print(person) {
  console.log(person["name"] + ", " + person["job"]);
}

This is fine, but potentially fragile, since person and print are defined seperately.

In an OOP methodology, you define them together:

class Person (name,job) {
  var name = name;
  var job = job;

  function print() {
    console.log(name + ", " + job);
  }
}

And now I can instantiate a Person whenever I need it…

var who = new Person("John Smith", "Doctor");
who.print();

This latter way is arguably better because as a person writing code that uses Person, I don’t need to know or define how it holds it’s values or prints, I just need to know how to create it and what functions I can call on the Person.

Hopefully these practical examples will help. Bear in mind that with some exceptions, most languages can be written in an OOP manner or not. This includes JS in the most recent incarnations. In some languages, like Java* or Ruby, you don’t have much choice: all things are objects aka instantiations of classes. Some other languages don’t include these concepts at all. Most support various paradigms of writing code.