Sunday 15 February 2015

Week 6: OOPs, I did it again

Here's another glimpse of my not-so-fascinating programmer life: object-oriented programming and my familiarity with the whole shebang.


The term "object-oriented programming," or OOP for the acronym-inclined, is something that I have been familiar with since learning Java in my grade 11 computer science class. It's a key concept in computer science, with the main focus being objects, of course!

I revisited this concept last semester in CSC108 as well as at the beginning of CSC148 while reviewing classes. Prof. Heap asked us to do a complementary exercise that involved object-oriented programming.

With that, we all took out a sheet of paper and designed a class using the following approach:
  1. Find the most important noun. This will be the class name.
  2. Find the most important attributes of that noun (or less important nouns).
  3. Find the operations for the noun to carry out (or verbs).
This approach can be considered linguistic, as we're dealing with nouns and verbs from a natural language (English) and putting it all together into a programming language (in our case, Python).

Here's a diagram for those who love looking at visuals (like me):



We had a few seconds to do it. I organized a sort of outline for this approach on my paper but was unable to identify the three parts in time. Eventually we took it up. (I would have liked a bit more time, personally, because I'm still not a proficient programmer yet and I'm a slowpoke.)

I'm familiar with Prof. Heap's teaching approach of having us complete exercises in a short period of time, but at the moment, it still would take me a while to solve these types of problems. When we take it up, however, I realize, "Of course. That's what is supposed to happen. Why didn't I think of that before?" To solve these types of problems within his given time frames (e.g. a minute), I'll do some review at home before coming to lecture. That's something I should have done in CSC165, but my course load was a bit too much for me to handle with five courses. Well, it's not too late, right? Then again, I have office hours to look forward to if I still don't get something. I will continue that approach so I can at least match my success in CSC165. (CS major, I'll see you soon.)

Anyway, I'll wrap this entry up by summarizing what I know about object-oriented programming.

We start with a class. A class is the most important object of any program. Using the approach that Prof. Heap provided, we start with #1, which is essentially the first line of our program. Here's a generalization:

class YourClassHere:
      """Your docstring goes here."""
      # to be continued...

At this point, our program is at a clean slate. Now, we want to implement steps #2 and #3 from our approach. How do we go about this? We need methods. Methods are functions defined within a class. Some important methods are: __init__, __eq__, __repr__, and __str__. We can also add our own methods, too.

Let's add on to our little generalization above.

class YourClassHere:
      """Your docstring for your class goes here."""

      # always start with __init__ to initialize your class!
      
      def method1(self, ...)
          """ (YourClassHere, ...) --> ...
           
          Your docstring for method1 goes here.
          """ 
          # body of method1
          # initialize variables, add loops, etc.

      def method2(self, ...)
          """ (YourClassHere, ...) --> ...

          Your docstring for method2 goes here.
          """
          # again, add body here
     
      # you get the idea

Another idea in object-oriented programming is inheritance. 




Here's the answer: Inheritance is the act of creating a subclass as an extension of the original class. The methods from the original are inherited onto the subclass. 

Let's inherit our class from above, YourClassHere.

class YourSubclassHere(YourClassHere)
      """Inherited from class YourClassHere..."""
      # inherit methods

In Assignment 1, we applied inheritance to our game_view program. We had general classes where the user could access the games made available to them, and specific classes for our game, "Subtract Square." Generally, inheritance is greatly applied when there is a general class and specific classes are needed as extensions.

The following diagram can add to our generalization of inheritance, too. The vehicle is the general class and the bike is the specific class (specific means of transportation that we are looking at). "Pedal," "trike," and "motor" are all subclasses inherited from the "bike" class. It's similar to the "Gameview" and "Subtract Square" classes from Assignment 1.





I think that sums up object-oriented programming. As well, this post also sums up object-oriented programming nicely, so feel free to check it out!

As always, feel free to leave a comment. I would love to hear any feedback from you.

Happy Reading Week, everyone! See you on the flip side!


8 comments: