Thursday, 22 January 2015

Why Use OOP?

Classes and Usefulness.

In class, we talked a lot about classes. I feel that the big question is why use OOP. Especially with the introduction of Stacks, Queues, and Assignment 1 in particular, many people just don't understand what their point is. For assignment 1... it's point is for us to learn, but in general, what are some advantages of using OOP?

I want to first outline briefly the conventional class design description and then discuss some of the advantages of classes.

Classes are containers. They contain information about something. Rather than having everything spread around everywhere, related variables and functions can be tied together. For example, we can have a dog object which might have a name, dog_type, weight, fur_status, and so forth as descriptors and bark, sleep, eat, poop, see, etc as is methods. The variables within the class describe the useful aspects of the object while the methods change the dog or do something depending on the dog.
We know that functions are useful because repetitive tasks are made easier and making changes later becomes easier. To elaborate on the latter, suppose that for sorting, faster algorithm was found. To make changes for all the things, all the positions where sorting was attempted needs to be found and then all of them must be changed. Also, parallel development is easier since the functions can be written simultaneously.

All the advantages of functions apply. You can make changes within the class methods or internal variables without affecting the external code that uses this class. Of course it also prevents one from needing to reinvent the wheel over and over again and allows parallel development.

An additional benefit of classes is evident in the above definition. It serves as a container, which groups concepts and breaks them down to facilitate implementation. Also interesting is the possibility of subclass usage.
The latter is similar to functions in that different functions can be passed. Let us analyze the following code:


Here, the function c is changed based on user input.

A similar task can be accomplished by using classes. Suppose you have a dog and a cat. You don’t know which one you might have… but you know they can both say hi… but the result of that should be different… The dog should output “woof” and the cat “meow.” This is where subclasses are useful. In a way, the act of being an instance of a particular subclass is in this case the instruction for the method say_hi.

Hope this was useful

No comments:

Post a Comment