What is Object Oriented programming?
I wrestled with this one in school for a while, because the name is so screwy. What is an object? It's a blob of code. It's a walled off place in your computer's RAM that has variables and functions. It's just a bit of reusable code.
Why is that special? If you've spent your programming life copying and pasting code in a text editor, it's probably not going to be readily apparent. It's pretty easy to reuse code without having to jump through OOP hoops.
In OOP, those reusable bits of code aren't copied and pasted, in the text editor, however. They're reused automatically in memory. When programming in an OOP style, you create one blob of code (an object), and then without touching the original, you can use the object and use it's methods and variables.
You can modify or extend the object, and you're never changing the original object.
For instance, in Ruby. Everything is an object. Even integers are objects and strings are objects. That is, every time I use the integer "5" in my code, I have all the methods from the Integer class available at my finger tips.
So, I can do something like this:
5.next #this returns 6
5.next.next # this returns 7
Or, I can use the "times" method from the Integer class, to create a loop:
5.times do |i|
print i, " "
end
#produces
0 1 2 3 4
Why can Ruby do this? Because all integers are a type of object that has the .next() and the .times() method that we can use every time we use an integer.
The same goes for strings. In Ruby, every string is automatically given all the methods from Ruby's String class for free. So, you can do stuff like this:
"hello".capitalize #this returns "Hello"
"HELLO".capitalize #this returns "Hello" as well
and
"hello".empty? #returns false
"".empty? #returns true
"hello".length #returns the length of the string
"hello".reverse # returns "olleh"
Every string that you use in Ruby has all the String class methods available to the programmer, because in Ruby, every string is an object.
There are lots of other things that you can do with OOP, this is just a taste of what Ruby does. I think the key to learning OOP is to learn it in a language that uses it very elegantly like Ruby, or even Python. If you're trying to learn OOP in PHP or Perl or Java, you're in for a rather difficult time, because there's so much other stuff that gets in your way.
But, if you learn OOP in a langauge that uses is really well, you can isolate some of the OOP concepts, and you can go back and use them in your language of choice if and when you need to.
OOP certainly isn't the be all end all of programming paradigms, but it's well worth your while to learn if you're going to be writing code for a living. It's one way to reuse code. It's certainly not the only way.