Sunday, April 11, 2010

OOPS-Building blocks

Here are the basic blocks:
  • Class
  • Objects
  • Methods
  • Inheritance
  • Polymorphism
  • Abstraction
  • Event
  • Encapsulation

Class defines the attributes/qualities of anything and the processes that it can carry out. It brings in the modularity to the software, which thus starts making sense to even a person who is not a programmer himself. This is because a class tries to emulate the real life properties/qualities of its wards (objects). For example, HumanBeing can be termed as a class, which specifies some of the common attributes of every human such as their ability to speak, walk, see etc. A class is self contained as well, thus changing anything in a class only affects it's objects.

Objects are the instances of a class. Objects have all the attributes of a class but with their own specifications. Such as CharlizeTheron is an object of HumanBeing and has all the characteristics of this class but is blonde. In object oriented programming, a programmer would say that the object CharlizeTheron is a run-time instance of the class HumanBeing.

Methods are the abilities/processes that an object has. Methods are designed for the entire class generally, but would only affect the object that refers to them. For example, CharlizeTheron can talk, hence talk is a method for class HumanBeing. Methods can further be broken down into commands, callbacks, or queries. A query will tell you about the state of the object, and a command will cause that object to carry out a task. A callback is a method that is composed of a part within an object. The part will notify its parent that an event has taken place, and the object may need to react to the event.

Inheritance A class can also be divided into sub-sets. For example, Female can be a sub-set of HumanBeing and CharlizeTheron is an object of Female sub-set. Every sub-set within an OOP language will share some attributes with the parent class, but it will also have unique attributes as well. Inheritance allows the sub-sets to link with the parent class. Lets say the class, HumanBeing has a method called talk() and a property called HairColor. Every sub-set will be able to inherit the properties and methods leading to the development effort reduced to just coding once for each such property/method. The traits can however be changed for the sub classes (sub-sets). Now, if we have two sub classes "Asian" and "European", we can set the default HairColor for "Asian" as black and something else may be blonde for "European". Another property may be Language which can have different default values.

We will continue the discussion in the next post.

No comments: