Sunday, 15 February 2015

Object-oriented programming concepts (week#6)

Object-oriented programming(a.k.a. OOP) is a programming paradigm based on 'object'. It's just like a model of the objects in the real world. We generalize and abstract the real-world objects, and then define them in classes using attributes and methods.

In our daily life, every object has its properties(states) and behaviors (for example, dogs have properties having claws and fur, and they have behaviors bark, eat and run), so does the 'object' in OOP. Basically, it has two main concepts(components):

1. Attributes(Properties)
2. Methods(Behaviors)

These two components are usually presented in the module of the objects -- class

As is taught in the lectures, when we see a handout for coding using python (a typical object-oriented language), we firstly pick up all the verbs and nouns, and then analyze them -- the verbs as methods and nouns as attributes.

Similarly, the first step in OOP is to identify the objects we want to manipulate and analyze how they are related to each other. Once an object has been identified, we generalize it as a class of objects.

Objects are instances of classes, and classes determine the type of objects. To be more specific, let's see the dog example I mentioned before:

Name of the class: Dog
Attributes: claws, fur
Methods:   bark, eat, run




In addition, OOP has a very important and useful property, that is, inheritance.

Inheritance makes it possible to define subclasses of data objects that share some or all of the main class characteristics without rewriting them.

Let's continue the dog example:

Subclass of the class Dog:

Name of the class: PomeranianDog
Attributes: small claws, golden fur, smart
Methods:   bark, eat, run, play

In the example, the Pomeranian is also a dog, and it has all the characteristics of dogs, so it inherits many things from its superclass (Dog) such as attributes claws, fur and methods bark, eat and run. However, since it is a specific kind of dog, it has more detailed things than a dog, which includes small claws, golden fur, smart and play. A subclass inherits some(or all) attributes and methods from the subclass and then extents or modifies the attributes and behaviors.

Inheritance provides OOP many benefits:

1. A programmer can write a subclass inheriting the common features from the superclass instead of changing the module or rewriting all of the code, which makes object-oriented programs easier to modify as well as saves a lot of time and effort.
2. It is reusable not only by the program for which it is initially created but also by other object-oriented programs, so that it is easier to distribute for use in networks.
3. It uses encapsulation(modular objects have their own data and are responsible for their own behaviors), which means changes in one object do not affect other objects, providing greater system security and avoids unintended data corruption.


references:
1. Celina's SLOG: My summary of object-oriented programming concepts
http://celinajiangslog148.blogspot.ca/2015/02/week-6-my-summary-of-object-oriented.html

2. Wikipedia: object-oriented programming
http://en.wikipedia.org/wiki/Object-oriented_programming

3. Margaret Rouse: object-oriented programming
http://searchsoa.techtarget.com/definition/object-oriented-programming

No comments:

Post a Comment