Sunday, February 15, 2015

Week 6: OOP-De-Loop

Object-Oriented Programming! What an important programming concept. Once I learned how to effectively implement OOP, I never wanted to go back to purely functional programming. However when I first learned about what an 'object' was, I was far from convinced.

I still remember it like it was yesterday - it was the night before my Grade 11 Introduction to Computer Science exam and I was on the phone with my friend who was going through an impressive list of metaphors to try and help me wrap my head around the idea of 'objects'. That phone call lasted about an hour and a half before I hung up, extremely frustrated. Why does everyone keep talking about recipes? And what does the human race have to do with classes? Long story short, I did the exam pretending to understand what an object was (and I did surprisingly well, considering). Two years later, I sit here in the very same spot I was sitting when I was on the phone trying to wrap my head around objects, with a complete understanding of classes and object-oriented programming. It took a summer of re-doing the Grade 11 ICS curriculum and three half-year courses, but finally here I am. And now I'm obsessed.

To explain the basics in a way that I wish I was taught, think of an object as a circle. And that circle has arms (you can have as many arms as you like - for this example, let's say there are 3 arms). Each of these arms represent a different feature about the circle; say, the circle's favorite movie or it's favorite color or it's last name - just features of the circle. Now, different circles may have different preferences of colors and movies but at the end of the day they are all circles with preferences.

Following this example, the class would be a Generic Circle with three arms. Since each of the above circles all came from the single Generic Circle (class), they would be objects of the class (each object is an instance of the class it belongs to). And finally, each of the arms are called instance variables, basically just attributes or 'preferences' of the objects. Often times you can make classes do things or give you outputs by creating functions within the class called methods, but more on that later. More complex ideas such as inheritance and super-classes will be elaborated on next week. I hope this helped you understand the conceptual idea of objects in relation to classes - we'll get more down and dirty with actual code soon!

No comments:

Post a Comment