Saturday, September 4, 2010

Object Oriented Programming

C# is an Object Oriented language. Even if it is not pure Object Oriented like Eiffel or SmallTalk, it was designed mainly for Objected Oriented programming. Ok, what is Object Oriented programming?
In Object Oriented Programming or OOP, we map identities in real world to objects and model the actions as methods. What takes priority in OOP is the object, as opposed to the actions related to it.
In OOP, there are a couple of concepts that we might ignore as programmers most of the time, while having a solid grasp of what these concepts are, is imporatning to create robust OOP application.

Level of abstraction:
In real life, we try to reduce the number or the complexity of things we deal with. It means we work at a specific level of details. In OOP terminology, this is called the level of abstraction. If you want to turn on the TV and watch a program, you are not going to have to open the TV set or reprogram the electronic chips in it, even if your expertise is assmebling television sets. This means you abstract away the details you don't want to get involved in.

Classification:
We classify almost everything to reduce the number of things we have to remember. If you are asked to use a brand new mobile phone, in spite of being unfamiliar with that particular phone, you will still be able to perform the basic operations on it, because at the end of the day, it's a mobile phone and you are likely to have already operated one. The new type of mobile phone shares most of the properties of other mobile phones. So we can say the phone inherits the common properties and actions from a general phone.
In C#, classification has been implemented by Inheritance. A class can derives from another one. This way, it inherits the properties and methods of the base class. The first class is a child of the second class (the parents class). The parent class can have several child classes, while each class can only have one parent class.
You may wonder how we benefit from Inheritance. In terms of OOP in C#, Inheritance introduces these benefits:
  • It reduces the amount of typing. If you have several classes that have some properties in common, you can create a base class defining the common properties and behaviors, then derive the other classes from the base class.
  • It facilitates reusability. Well, you shouldn't reinvent the wheel. Starting a software from the scratch is a pain in the neck, while you could use already existing components. By using Inheritance, you can adapt existing classes to your program while leaving the internals of the classes intact.
  • Updates will be integrated with much less hassle. If you change a base class, the change will be reflected in all child classes as well.
Polymorphism:
Late binding or polymorphism is the ability to decide the type at run time. This concept is so important that if a language supports Objects but not polymorphism is called Object Base (such as Ada).
Suppose you have a base class and a sub class that inherits from it.

public class baseClass
{
      int prop1;
      int prop2;
      public int Prop1{get{return prop1;}set{prop1 = value;}}
      public int Prop2{get{return prop2;}set{prop2 = value;}}
      public virtual int Operation()
      {
            return Prop1 * Prop2;
      }
}

public class subClass : baseClass
{
      public override int Operation()
      {
            return Prop1 * Prop2 / 2;
      }
}
 
...
 
public void Test()
{
      baseClass b;
      b = new subClass();
      b.Prop1 = 4;
      b.Prop2 = 3;
      int result = b.Operation();
}
 
In method Test, object b is a reference to baseClass while it actually contains an object of type subClass. So, to the compiler, it is a baseClass, while at run time, polymorphism will let you take the appropriate type. The method Operation is a virtual method, which means it can be overriden in the sub classes.
If you run this code, the result will be 6, not 12. That's polymorphism! At run time, the most appropriate type was chosen, which was baseClass and its method was executed. Of course, if you don't override the method and hide the method by using the keyword 'new', the result will be different. This happens because you break the chain of hierarchy by using 'new'. You can check it out by debugging and stepping into the method.

No comments:

Post a Comment