Wednesday, January 22, 2014

Object-oriented Programming

Being relatively new to programming, the concept of object-oriented programming does seem to be quite intimidating. In my previous CSC course, objects (instances of classes) and their methods was introduced to us but it was not covered or focused on in great detail and therefore my comfort level of writing classes is not where it should be at. In spite of that practicing by writing will only improve my efficiency of writing code but also get a better understanding of classes to method interactions. (i.e. not forgetting to put self as a parameter when defining a method of a class).

Object-oriented programming can be seen as focusing on the creation of objects which contain both data and functionalit. These object defintions can coresspond to real world objects or concepts and the functions that operate on these objects represent the way that real world object interact. 

By week 3, we have jumped into topics on inheritance. At first the concepts did seem quite confusing at first but after going over notes and readings, understanding why inheritance is important and why it is used was key to understanding the concept. Some of the things i learned are:
  •  if one wanted to modify an existing class to change or add functionality, one cannot just directly make changes as it would break the existing clients. 
  • one of the solution would be to create a subclass of the base class (i.e. class Parent(object): would be the base class while class Child(Parent): would be the subclass).
  • by default, the Child class will inherit all the methods from the Parent class and therefore the Child like is exactly the same as Parent class except with the desired changes applied such as adding or modifying behaviour.
  • adding or modifying behaviour in the Child class can result in overrides. This means that whatever desired changes to methods and behaviour in the Parent class can be applied to the Child class by naming the method with the same name in which the Child class will override the Parent class.
What comes after concepts? Coding of course.  Aside from solving the problem as hand, wiritng the bodies of the methods of classes, was much the same and understandable. However, with the problem of defining a new Stack class that handles only integers, errors are often going to arrive when the user inputs a non integer. However, this time i am the one designing the error that is being raised. This is of course something new to me and i quite enjoyed knowing that we were able to raise our own exceptions, even though the steps to creating our own exceptions was incrediable easy, as it only consisted of creating a new class named whatever exception name we wanted and putting in pass.

No comments:

Post a Comment