Saturday, 12 October 2013

Object-Oriented Programming

In CSC148 we are learning to program in Python3, an object-oriented programming language. What is object-oriented programming? That's an easy question to answer: "Object-oriented programming (OOP) is a method of programming that is oriented on objects!" Not a satisfying answer. A more explicit, and less unsatisfying, answer is: "Object-oriented programming (OOP) is a method of programming that focuses on representing objects with code by defining different properties (attributes) that the objects have and actions that can be done with the objects (methods). The objects are represented as particular cases of a blank representation of the object (a class)." This definition still seems pretty vague and general. Almost anything can be thought of as an object, and each of these can have all sorts of properties and things that can be done with them. Classes, blank representations or blueprints for objects, are even more abstract and general. Python, however, isn't the first programming language I have been exposed to. The first language I learnt was C# (also object-oriented), which I took a class for two years ago, and although from the beginning of the class I was aware of a similar definition of OOP, it took me a long time to really understood what it meant and it wasn't until the beginning of this semester when it all started to make sense to me.

In my first class in computer programming, we began by learning how to write some very basic programs without the use of classes and objects. When classes were first introduced in the course I had a very difficult time understanding what was going on. There were lots of new terms, instance constructors, and I had a hard time comprehending what was happening when objects, which are instances of classes, where being constructed. It also took a lot of work and time to create classes for the extremely simple objects we were considering in our examples. This completely obscured the motivation for classes in the first place! Instead of making the programs easier to think about, write, and modify, using OOP made everything more complicated. However, after being forced to use it and after beginning to use it to write programs to solve more complex problems, OOP gradually made more sense.

At the beginning of this semester, when relearning the basics of OOP in Python, it all really clicked. Our recent assignment on animating an interactive game of Reve's puzzle as well as an automated solution, which was a rather complicated problem, really required the use of OOP to simplify the programming and helped solidify the concepts involved in writing object-oriented programs and the benefits of OOP. I now realize the brilliance of the programming paradigm and how powerful it is for solving problems. The physical world is made up of objects that have properties and have actions done to/with them and consequently, humans naturally think and speak in terms of objects, their attributes, and their methods. Thus representing and using objects in programs is a very natural way to write programs about almost anything you can think of. In the physical world it is also often the case that many specific objects are closely related to each other and are members of a large class of objects, this class of objects might be closely related to other classes of objects, all of which are more specific examples of a larger class of objects, and so on. Creating classes of objects and using inheritance to create subclasses is naturally modelled in OOP. Finally, mathematical objects and mathematical structures, which are a very important aspect of computing, are also well-suited to OOP.

To somebody new to OOP, the definition given at the start of this entry might seem so general that it hardly says anything at all about what OOP really is. However, the definition does a good job of capturing the generality of what can be coded in an OOP language, which is one of its main strengths.

No comments:

Post a Comment