Tuesday, 17 February 2015

Object Oriented Programming

Object oriented programming is a very useful tool for computer scientists.  Python (and other languages) have built-in objects or classes which give the user simple data types to work with.  Examples of these are integers, strings (which function as sentences), and floats (numbers with decimals), as well as more complex structures such as lists of objects, and dictionaries (one type of object is paired with another type or a list).  Object oriented programming allows a user to create their own classes/objects, which can provide more detailed and user friendly programs.  A programmer can also write specific methods for their own classes, which can also make these data structures easier to access and manipulate.  These classes use the basic python built in data structures, but can combine them more easily, keeping all the data together in one convenient place.

An example of a user created class that we saw in lab was a Patient Roster for a doctor.  This class was created with a setup for a dictionary of the actual patients, but also built in restrictions (gender ratio and maximum number of patients).  Additionally, we wrote a method to add patients to our class which checked if the gender ratio or maximum number was violated before adding the patient.  This eliminated a lot of steps that a user would have to take themselves when using basic data structures, and made things much more simple.

In addition to creating classes, a user can create subclasses, which are a part of the superclass.  These subclasses will inherit the methods which are written for the superclass, and they can have separate specific methods written for themselves.  An example of this we saw in lecture was the superclass Shape, with its subclasses Triangle and Square.  Shape was initialized with a position, and had a method to change its position.  These properties were inherited by both Triangle and Square, but they each had individual methods called draw and get_area, which were specific to themselves.

When a subclass inherits a method from a superclass, there are 3 things that can happen.  The method can be: inherited, extended, or overridden.  Normal inheritance was discussed above, where the method in the superclass works for the subclass.  The method can also be extended, meaning it does everything it does in the superclass, but also some more.  Lastly, the method can be overridden, meaning the method in the subclass is completely different than the one in the superclass. 

All these properties of object oriented programming allow for a computer scientist to create very specific structures and methods.  This specificity is extremely helpful for future use of the program, and one would not be able to replicate the specificity using the built-in Python data types.

1 comment:

  1. More than being a tool, OOP is a "paradigm." It's a particular approach to designing software and creating abstractions in the code for the storage and processing of data. Once you get past the initial learning curve, it allows you to think about your programs in a very intuitive and more concrete way - in terms of "objects."

    ReplyDelete