Sunday, 15 February 2015

Summary of Object-Oriented Programming Concepts!

For this weeks post I'll be summarizing object-oriented concepts! In CSC148 we've been using python and it's a very object-oriented language. The course has definitely been helping me understand it a lot more because we've been using it so much in all of our lectures, labs, assignments and tests. I'll first be introducing objects and classes. I'll talk about some important terms related to them, talk about the public interface and then I'll move onto some inheritance.

Object oriented programming is essentially based around objects. An object bundles together data  as well as the methods that can be applied to it. They're stored at a location in memory. In order to actually create an object, we will need a class, which is considered a blueprint for making objects. Each class has attributes (data associated with the class) and methods (or operations). To create an instance (a specific realization of any object) of a class, I have to instantiate it by using the special method __init__ in python. There are many other special methods in python (certain methods available when you use a particular syntax), although in this course we've specifically looked at __init__, __repr__, __eq__, and __str__. We can also override these and define them ourselves if we'd like. In class methods, you always have the parameter self, and this self refers to that specific instance upon which you are calling to use those methods on.

Now that we know what those terms mean, we can now talk about the public interface, which is essentially the features of the class that other code may use. Specifically in python, there's no notion of attributes being private and so it's possible for them to be changed outside of the classes in the client code (the code that runs the classes), which is something we don't want. Something we've learnt in lectures is to use the property mechanism to avoid this by using a specific method to set the value and another to get the value.

Now lets talk about  inheritance. Perhaps we want to create more specific class that has some features of the another class. It would be a bit redundant to retype the entire class again and this can also allow for more room for errors. Instead of doing this, we can create a sub class of the parent class in which the subclass inherits all the methods and attributes that we've assigned for the parent class. It can be thought of as a hierarchy system, with the parent class being a lot more abstract and the sub classes being more specific. For certain attributes you can extend upon them in the sub class and for certain methods you can extend or completely override it if you can't find a way to reuse the code from the parent class.

So here is my short summary of object-oriented concepts. We used it heavily in our first assignment especially when we had to design a program using different classes to handle different games and strategies. I got really good practice because of it by trying to figure out specifically what methods and attributes we had to have and how to organize the program from a more abstract level in the parent class to a more specific one in the sub class. I find it's a pretty neat way of organizing information and very useful as well. Hopefully from my summary you readers can get a good general idea of these concepts as well!

No comments:

Post a Comment