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")
friend1.show_birthday()
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.