Thursday 23 January 2014

Object-oriented Programming

At the end of CSC108 last semester, we started learning about classes and methods (Object-oriented Programming!). I found it extremely confusing and didn't really understand why we were learning it. I thought listing functions was good enough for what we were doing, and it seemed like object-oriented programming was more work then it was worth. Now I'm starting to learn about all the benefits of it and how much time it really saves. I also never really realized how relatable programming can be to the real world.

For instance, lets take a class Animal. Just like real world animals that do things like eat and sleep, Animal would naturally have within it methods that apply to all animals, such as Eat, Sleep, Poop, etc. But what I find the most interesting about object-oriented programming is when we create subclasses. Say we wanted to create multiple animals, for example lets take a Rat and an Eagle. Naturally rats and eagles are animals so they would inherit all the same attributes that apply to Animal. This is such an efficient way of programming since now we can focus on actions that are specific to rats and eagles and we don't have to rewrite any code that already exists in Animal. For example eagles can fly and hunt, but not all animals can, so in our Eagle subclass we would have the methods Fly and Hunt. Likewise for Rat we could have methods such as Scavenge and Run. Furthermore whats cool about object-oriented programming is that if we wanted to get more specific about things like how much an Eagle sleeps, we could do that within Eagle and not modify the original code.

This example shows how object-oriented programming can relate to the world around us and how efficient it can be by recycling code. I'm excited to explore more aspects of it in the coming weeks.