Monday, February 9, 2015

Week 6: Object Oriented Programming

Please head over to https://nanalelfecsc148slog.wordpress.com/2015/02/16/summary-of-object-oriented-programming/ for an interesting post on OOP!

In week 6 of CSC148, we have been assigned the topic of summarizing Object-Oriented Programming.

Below, I will share a quick summary of OOP in jot-note and sentence form.

The Object-Oriented Paradigm

Object-Oriented Programming emphasizes objects (data) over actions. This is in contrast to procedural programming, where programs are approached by decomposing problems into a series of actions (functions).

In OOP, we approach a problem by decomposing it into data types. The difference is what we consider first (data before actions vs actions before data).

Objects in Python:
Class (data type definition): a blue print for creating objects
- field/attribute (data member)
- method (function member [action], operations for acting upon data)

Instantiating an Object
An object/instance is a piece of data.

When we instantiate a class,  each instance has its own copy of the fields, but not its own copy of the method.

Principles of OOP

[1st Principle]
Encapsulation: The fields of an object should only be read by methods of that instance's class.
 
- Methods act as "interface" to object's field
- Idea that when you create a class, the methods that you give that class encapsulate (encompass) all of the knowledge that is meant to be done to the data that makes up that data type (modulate data)

[2nd Principle]
Inheritance: The idea of inheritance is that some data types/classes may overlap, such that one data type has all of the same methods/fields found in another.

- The child class will inherit all of the methods/fields of the parent class (including any attributes/methods that the parent class has inherited itself)
 - Child/Descendent = subClass. Parent/Ancestor = superClass

Abstract data types can be a bit confusing to identify as supertypes/subtypes. Think before using inheritance. is-a (a car is a type of vehicle) vs has-a (cars have steering wheels, but a steering wheel would not be a subclass of a car. a steering wheel is not a type of car). Extending/composition.
 
In some OOP languages, every Class is required to inherit from one other class. By default, this Class is a special Class called the object Class, which is built into the language.

Overriding: To override in OOP means to redefine an inherited method.

Invoking methods: object.method(args)

Polymorphism
x.foo()
Polymorphism: depends on type x

Class member (member of class itself, not any instance)
- Class.field
- Class.method(args)


Summarizing Privacy and Property in OOP (Jot-notes)

- Python has no notion of attributes being private, so they're all accessible to clients, but Python has a property mechanism to redirect access to an attribute so that it muse use a method

- property() function:
    - create a method to set x to its initial value. can only be done during initialization. check whether             the name _x is already defined in object self
    - def set_x(self,x):
        if '_x' in dir(self):
            raise Exception('Cannot change')
        else:
            self._x = float(x)
    - next, make sure anybody wanting to use value of attribute x actually uses the value referred to by     _x:
    - def get_x(self):
        return self._x
    - Now, we must tell Python to redirect all atempts to assign to or evaluate x using applicable    
    methods
    - property() ==> takes 4 arguments: method to get value of x, method to set value, method to del         attribute x, and some documentations (arguments can optionally be done)
    - x = property(get_x, set_x, None, None)
    - now: all client code that evalutes or assigns to x is redirected to get_x and set_x

Optionally, you can use @ headers:

For example:

class Example(object):
    def __init__(self, value):
        self.x = value
    @ property
    def x(self)
        return self._x
    @ x.setter
    def x(self, value):
        if '_x' in dir(self):
            raise Exception('Cannot change')

These headers accomplish the same objective as the property function. Now, even the code we have written in the method of class's init gets redirected.

Overall, I believe that I have a strong inclination towards OOP versus Procedural Programming, as I prefer approaching problems by decomposing them into data types rather than decomposing them into functions. Python is an extremely accessible and intuitive programming language that is an excellent introduction to the world of programming.

No comments:

Post a Comment