Computer Science Canada What is Object Oriented Programing?! |
Author: | B-Man 31 [ Mon Nov 09, 2009 7:27 pm ] |
Post subject: | What is Object Oriented Programing?! |
Hey, i was wondering if someone could explain to me who OOP works, i believe my current style is procedural programming but im not sure 100% what either is, any clarifications would help. I have wikipedia'd it but its not making much sense to me so if anybody knows how to explain it an a easy-to-understand way, i would greatly appreciate it ![]() |
Author: | Tony [ Mon Nov 09, 2009 7:54 pm ] | ||||
Post subject: | RE:What is Object Oriented Programing?! | ||||
Super short version: In "procedural" paradigm, you have a world (your program) filled with global types and functions. You might have something like:
OOP allows you to create a system of classes that models the world in terms of "objects". Everything is an "object" and those objects can perform "methods". Ultimately it will allow you to write code in a form of
And the pet will figure out on its own if it's a cat or a dog, and how it should speak. --------------- Obviously the above leaves out most of the details, but ultimately different programming paradigms (see http://en.wikipedia.org/wiki/Programming_paradigm) are about different ways of representing elements of the program, and defining their interactions. |
Author: | Superskull85 [ Mon Nov 09, 2009 7:56 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
I like to think of OOP as the ability to box off different groups of information. For example, you can think of water as an object. You can drink the water, boil the water, put the water in a container, mix other liquids with it, etc. Unless you are a chemist, you don't care how it is composed, why it does what it does or why it even exists. All you care about is that it can keep you hydrated and you are able to use it to create something else. Another example is creating a "Rectangle" object. The rectangle could of been created using four lines, a fill colour and used an external class. However, all you care about is that it can be drawn to screen, you can move it around, change the colour, change the size, etc. Basically each class is able to create an object that you can do stuff with. You do not want/need to worry about how the object was created, or why it works (unless you are expanding on a class), as it does what it is supposed to do. There is a lot of different concepts in OOP, so if you have a specific problem I can provide a more specific analogy/answer. |
Author: | B-Man 31 [ Mon Nov 09, 2009 7:59 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
no, i dont have any more questions, is there any tutorials on how i could learn OOP here on compsci, i heard its a lot more efficient. And i think im starting to understand. as much info would be appreciated. Thanks Tony and Superskull85 for you input. |
Author: | Insectoid [ Mon Nov 09, 2009 8:08 pm ] | ||||||||||
Post subject: | RE:What is Object Oriented Programing?! | ||||||||||
That's a question that has stumped many a new programmer. You've been doing procedural programming. That means you have one program that holds every variable, every procedure and function. Almost anything can do anything to anything. It's rather linear, and you can follow through with it from the top down. Object-oriented programming is slightly different, in that there is a main program which controls most things, as well as 'objects'. These can be thought of a sub-programs that you can use in your code. These objects are 'imprints' if you will of a class. a class is a template for an object. A common example is the String class. Classes can contain functions (also called methods and other things), as well as straight data (aka variables). An object is an instance of a class. Kind of like a cookie cutter. A class is the cutter, and it can make many cookies. For example, we have made a Circle class. It has a variable for the radius. It also has several functions to calculate the circumference and area. With this, we can create a number of different circles of different radii and it will do the rest of the work for us. Let's have some pseudo code:
Now, we can create an instance of this class, an Object:
We have now created a circle with a radius of 5. We can access this information with dot notation. This is different from procedural programming. You're probably used to:
dot notation uses a dot (.) to call functions within a class
Now, let's make another Circle;
I'm calling the area function, but from the baz object. It will return 2*3.14*10, whereas foo.area will return 2*3.14*5. This is a very brief explanation, I hope I got something across. I think the equations might be mixed up. I can never keep them straight. |
Author: | DtY [ Mon Nov 09, 2009 8:09 pm ] |
Post subject: | Re: RE:What is Object Oriented Programing?! |
B-Man 31 @ Mon Nov 09, 2009 7:59 pm wrote: no, i dont have any more questions, is there any tutorials on how i could learn OOP here on compsci, i heard its a lot more efficient. And i think im starting to understand. as much info would be appreciated. Thanks Tony and Superskull85 for you input.
What language do you want to learn? Do you already know a programming lamguage, it might already have some OOP. If you don't already know a language that has OOP, or want to start fresh, learn Ruby. In my experience, it is the "most" OOP language. I'm not sure where to find it, but my first experience with Ruby was an online Ruby interpreter that ran in the browser, and had a tutorial that walked you through various things you can do, without having to download anything. If anyone knows where to find that, it would be appreciated. |
Author: | Insectoid [ Mon Nov 09, 2009 8:10 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
Ah hell, you guys fit 3 posts in while I was typing up mine? I am getting slow. Or long-winded. I dunno which. |
Author: | DtY [ Mon Nov 09, 2009 8:12 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
insectoid's description is really good, but I feel the need to point out that that is specifically class based OOP, which is probably what you will be working with, unless you use Javascript. (Which is prototype based OOP) |
Author: | Insectoid [ Mon Nov 09, 2009 8:14 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
Well, I've only ever used class-based OOP. And I recommend learning OOP with Ruby. Java was a pain in the ass for me, as it tries to do far too much for you and confuses the hell out of you. Ruby is simple, and yet somehow superior to the far more popular (and criticized) Java. |
Author: | Tony [ Mon Nov 09, 2009 8:16 pm ] |
Post subject: | Re: RE:What is Object Oriented Programing?! |
DtY @ Mon Nov 09, 2009 8:09 pm wrote: an online Ruby interpreter that ran in the browser
http://tryruby.sophrinix.com/ Edit: also Ruby is completely Object-Oriented (Smalltalk style), while Java still has non-object native types (int vs. Integer). Edit2: by "popular" you obviously mean "more widely used". |
Author: | B-Man 31 [ Mon Nov 09, 2009 8:30 pm ] | ||
Post subject: | Re: What is Object Oriented Programing?! | ||
instectiod, thank you so much, im starting to understand even though the code you wrote it in is C++ i believe, it still makes sense because i know basic C++, i was mainly wondering for Turing because i am getting bored in class and its seems too easy. no matter how many things i try to add. i decided learning OOP might be a good way to challenge myself. You guys seem to know quite well what your doing so in turing your liitle program would be like:
This doesn't seem to work so can anyone tell me what im doing wrong, thanks ![]() |
Author: | Tony [ Mon Nov 09, 2009 8:35 pm ] | ||
Post subject: | RE:What is Object Oriented Programing?! | ||
It's not C++. Quote: Let's have some pseudo code I think you might be looking for something like
|
Author: | B-Man 31 [ Mon Nov 09, 2009 8:38 pm ] | ||||
Post subject: | Re: RE:What is Object Oriented Programing?! | ||||
Tony @ Mon Nov 09, 2009 8:35 pm wrote: I think you might be looking for something like
Why Thank you, that did the trick ![]() This is basically what i have now, this is OOP then (very simply of course)
[EDIT:] Also can anyone tell me the advantages and disadvantages of OOP? |
Author: | Insectoid [ Mon Nov 09, 2009 9:07 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
I find OOP is a lot more organized if used well. You know exactly what is what and what can do what. Procedural programming can quickly turn into a mess since there's not much in the way of organization. |
Author: | B-Man 31 [ Mon Nov 09, 2009 9:12 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
now i am starting to understand for basics like this circle class, but what if you wanted to make a more visual and bigger harder program, how would you use OOP/classes? |
Author: | Insectoid [ Mon Nov 09, 2009 9:13 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
You could have a class that controls physics, one to control display, one to hold say, player data (a data object), etc. |
Author: | Tony [ Mon Nov 09, 2009 9:41 pm ] | ||||||
Post subject: | Re: RE:What is Object Oriented Programing?! | ||||||
B-Man 31 @ Mon Nov 09, 2009 8:38 pm wrote:
You only need one instance of a circle, you should not create a new one in every loop.
But still, this feels fairly procedural, as the circle is simply a container for some functions, and doesn't have a concept of self as an instance. A better approach would be to have something like
|
Author: | B-Man 31 [ Mon Nov 09, 2009 9:45 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
Alright, thanks. Actually, i want to say thank you to everyone that helped me out, you guys cleared up a lot, now that im am starting to understand, my friend pointed out there is a 3 part tut on classes, which i have already engaged in reading thoroughly. thanks again to all, you are real gurus ![]() |
Author: | Superskull85 [ Mon Nov 09, 2009 11:10 pm ] | ||||
Post subject: | Re: RE:What is Object Oriented Programing?! | ||||
B-Man 31 @ Mon Nov 09, 2009 8:38 pm wrote: ...Also can anyone tell me the advantages and disadvantages of OOP?
One advantage is that you can have multiple instances (copies if you like; although not entirely accurate) of similar groups of data. You could than change the behavior of a common operation, and add a new group of data, without having to change too much and possibly create an error. For example you could have:
Or you could use OOP and have:
If you wanted to change how new names were assigned, and add a new player, which piece of code do you think would be easier to modify? Another advantage is that you do not need to worry about the operation of each object interring with one another. An object would simply ask another object to do something and it would see if it is able to do it, if not it returns an appropriate response. The first object would not be able to accidentally modify the data of the second object. There are most likely disadvantages to OOP as well, I just can't think of any specific examples at the moment. |
Author: | Tony [ Mon Nov 09, 2009 11:43 pm ] |
Post subject: | Re: RE:What is Object Oriented Programing?! |
B-Man 31 @ Mon Nov 09, 2009 8:38 pm wrote: ...Also can anyone tell me the advantages ... of OOP?
Polymorphism |
Author: | B-Man 31 [ Tue Nov 10, 2009 8:17 am ] |
Post subject: | RE:What is Object Oriented Programing?! |
Again, thanks so much ![]() |
Author: | jbking [ Tue Nov 10, 2009 9:35 am ] |
Post subject: | Re: What is Object Oriented Programing?! |
Another advantage of OOP is encapsulation, also known as hiding the details. That is to say while you know what a method takes in as parameters and returns, you don't know the details of the implementation and that may be hidden intentionally. This was mentioned earlier but I don't think the term for it was given. |
Author: | DtY [ Tue Nov 10, 2009 3:55 pm ] |
Post subject: | Re: What is Object Oriented Programing?! |
jbking @ Tue Nov 10, 2009 9:35 am wrote: Another advantage of OOP is encapsulation, also known as hiding the details. That is to say while you know what a method takes in as parameters and returns, you don't know the details of the implementation and that may be hidden intentionally. This was mentioned earlier but I don't think the term for it was given.
This is more a feature of functional programming, though that's really what you're doing in OOP, just making a new namespace to do it in. |
Author: | Tony [ Tue Nov 10, 2009 4:06 pm ] |
Post subject: | RE:What is Object Oriented Programing?! |
The fun part to realize is that with encapsulation, you are hiding the details from yourself only (or from other developers on your team, or users, etc.). It's a social thing. The compiler doesn't care. The compiler has access to everything, and could pull those hidden details right through the interface, if it deems so to be more efficient. |