Wednesday, March 10, 2010

Object Oriented Programming Concepts

What is OOP?
OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts.
In order to clearly understand the object orientation, let’s take your “hand” as an example. The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it.


What is an Object?
An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address.
In pure OOP terms an object is an instance of a class.

What is a Class?

A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.

public class Student
{
}

According to the sample given below we can say that the student object, named objectStudent, has created out of the Student class.

Student objectStudent = new Student();
In real world, you'll often find many individual objects all of the same kind. As an example, there may be thousands of other bicycles in existence, all of the same make and model. Each bicycle has built from the same blueprint. In object-oriented terms, we say that the bicycle is an instance of the class of objects known as bicycles. In the software world, though you may not have realized it, you have already used classes. For example, the TextBox control, you always used, is made out of the TextBox class, which defines its appearance and capabilities. Each time you drag a TextBox control, you are actually creating a new instance of the TextBox class.



A Class basically contains the following:-
Attributes:- Any data,constants,types declared within a class form the attribute of the class.
Methods:- Block of code, providing some functionality offered by the class. Can be compared to function modules.
Events:- A mechanism set within a class which can help a class to trigger methods of other class.
Interfaces:-Interfaces are independent structures that you can implement in a class to extend the scope of that class.

Basic feature of Object Oriented Programming as follows :

  1. Encapsulation.
  2. Polymorphisms.
  3. Inheritance.

Encapsulation :
Encapsulation is a technique of linking together attributes and procedures to form an object . The only way to access attributes and procedures of an object is to create an instance of the object.
Some people may think encapsulation is simply a clever way to make your program orderly by putting related attributes and procedures under one roof. Although this is true, protection is the reason for encapsulation.

Protection Mechanisms :

  1. Public Access Specifier
  2. Private Access Specifier
  3. Protected Access Specifier

Example :
You are an employee (object of class:- employee). You have put your personal pens on the desk . These pens can be accesses by you(object of main class) , our son and daughters( sub-classes) as well as by any other people(users outside the class). So, your pens are in your public section.
You have a purse(attribute) in your pocket. Any other person (user outside the class) cannot use your purse(attribute) directly. But, your children(sub-classes of the class on which you are an object) can have full access to it.So, money in the purse is in your protected section.
You have a special skill which you have developed by putting constant effort for a long time. You can only access it. Neither your children(sub-classes of your class), nor any user have any access to it directly. So, you can consider that skill to be in your private section.

Inheritance:

Ability of a new class to be created, from an existing class by extending it, is called inheritance.

public class Exception
{
}


public class IOException
: Exception
{
}

According to the above example the new class (IOException), which is called the derived class or subclass, inherits the members of an existing class (Exception), which is called the base class or super-class. The class IOException can extend the functionality of the class Exception by adding new types and methods and by overriding existing ones.
Just like abstraction is closely related with generalization, the inheritance is closely related with specialization. It is important to discuss those two concepts together with generalization to better understand and to reduce the complexity.
One of the most important relationships among objects in the real world is specialization, which can be described as the “is-a” relationship. When we say that a dog is a mammal, we mean that the dog is a specialized kind of mammal. It has all the characteristics of any mammal (it bears live young, nurses with milk, has hair), but it specializes these characteristics to the familiar characteristics of canis domesticus. A cat is also a mammal. As such, we expect it to share certain characteristics with the dog that are generalized in Mammal, but to differ in those characteristics that are specialized in cats.
The specialization and generalization relationships are both reciprocal and hierarchical. Specialization is just the other side of the generalization coin: Mammal generalizes what is common between dogs and cats, and dogs and cats specialize mammals to their own specific subtypes.
Similarly, as an example you can say that both IOException and SecurityException are of type Exception. They have all characteristics and behaviors of an Exception, That mean the IOException is a specialized kind of Exception. A SecurityException is also an Exception. As such, we expect it to share certain characteristic with IOException that are generalized in Exception, but to differ in those characteristics that are specialized in SecurityExceptions. In other words, Exception generalizes the shared characteristics of both IOException and SecurityException, while IOException and SecurityException specialize with their characteristics and behaviors.
In OOP, the specialization relationship is implemented using the principle called inheritance. This is the most common and most natural and widely accepted way of implement this relationship.

Polymorphisms:

Polymorphisms is a generic term that means 'many shapes'. More precisely Polymorphisms means the ability to request that the same operations be performed by a wide range of different types of things.
At times, I used to think that understanding Object Oriented Programming concepts have made it difficult since they have grouped under four main concepts, while each concept is closely related with one another. Hence one has to be extremely careful to correctly understand each concept separately, while understanding the way each related with other concepts. In OOP the polymorphisms is achieved by using many different techniques named method overloading, operator overloading and method overriding.

No comments:

Post a Comment