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 + " was born on " + friend
But if you use a class:
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:
beth = new Family()
beth.name = "Elizabeth"
beth.birthday = "January 1st"
beth.relationship = "sister"
Now, you have the
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
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.