Friday, February 20, 2015

Oops… Here Comes OOP

Hi there, I am back to my 4th slog. As I mentioned in my very first post, why geeks should write, programmers need to continuously reflect and summarize what they have learned and encountered in programming. Speaking of this, I think now is a good time for me to summarize my understanding of object-oriented programming, which is the most important concept we have encountered in CSC148 so far. 
Please feel free to correct me and fill in by commenting below, as I know there are a lot to include. Also, I am excited to hearing feedback; )

Object-oriented programming (OOP), as the name suggested, is a style of programming in which data and the operations are organized into objects. In other words, programmers focus on objects instead of the implementation details. To understand OOP, I will walk you through a couple of key concepts with examples.

First of all, let’s talk about object. An object can be anything in the real world, like a pen, a car, a dog, or a subtract square game state. Each object bundles together attributes (data stored in variables) and behaviours (methods that perform tasks). For instance, a dog object have attributes: weight, colour, breed; and behaviours: barking, wagging tail. Now, let me give a formal definition of object.
Object: A basic entity in Python that models a thing or concept in the real world, which contains attributes and behaviours.
In fact, every value in Python is an object, like str, int, list and dictionary.

Next, we look at class.
Class:  A template or blueprint for the objects of similar types.
You can think of class as a type or a collection of objects as well.

Another important concept in OOP is inheritance.
Inheritance: A mechanism involves classes inherit (get) commonly used attributes and behaviours from other classes. A subclass (child) class, by default, inherits all the methods and attributes of the super (parent) class.

To illustrate these concepts, let’s say you want to create a circle, a square and a triangle. These are different types of shape, and you know they will be drawn at a certain position with a certain area. Instead of creating them separately, we will use inheritance and define a parent class, Shape.

As you can see in the doctstring , Shape is an abstract class that cannot be instantiated (I’d better define instantiation)
Instantiate: To create an instance of a class.

Notice that the get_area method raises a NotImplementedError, this is because different shapes have different ways of calculating the area, and we do not know how to calculate the area yet.

Now, if we want to define the Square class, we can simply make it a subclass of Shape, so that it can inherit all the attributes and behaviours of Shape.



Here, Square extends Shape, by adding the attribute base. At the same time, Square overrides Shape, by re-defining the method get_area. In fact, when we implement the special methods, __str__, __eq__ and __repr__ in a class, we are overriding these methods as well. The reason is that these methods are inherited from the ultimate ancestor, object.

To access the attribute or method of a specific object, follow the format:
Thingee.attribute / thingee.method()
Remember that Python starts by looking at the most specific part of the object and if it is not found, Python goes “up” to its ancestors.



As you probably notice, inheritance is a powerful structure that simplifies the code by giving Square all the attributes and behaviours that a Shape should have, yet allow it to focus exclusively on the features that make it unique. In fact, inheritance provides reusability, which means adding additional features to an existing class without modifying it. With this powerful tool, codes can be reused to create different kinds of shapes, like this.


The Java Tutorial website provides a great list of the benefits of OOP programming that I really want to share with you (the website is here).



To summarize, OOP programming is a revolutionary concept that enables problems to be analyzed in terms of objects and communication between them. With OOP, reusability of codes is maximized; different objects can interact with each other without knowing the specific implementation details. Below are pictures that further illustrate OOP.
Hope you enjoy reading this and please don’t hesitate to comment below.

Happy Reading Week Everyone!!!!

No comments:

Post a Comment