Four Pillars of OOP

We all use OOP languages in daily life but we still find ourselves confused about few important concept of Object-Oriented Programming. So let me put few of those here to clear the air.
So the four pillars of OOP that are essential to achieve re-usability, managing complexity, information hiding etc are:


  • Encapsulation
  • Abstraction
  • Polymorphism
  • Inheritance

Encapsulation and Abstraction:



  • Many people are confused about these two topics. We all write the code but still don't know if we achieve abstraction and encapsulation in it. First thing we should understand is that these are general daily life concept we try to implement in our software (or many other things). We just need to correlate those with programming.

  • So lets take an example that we often see while reading these concept. Consider your cell phone. You use mobile phone in everyday life but you don't need to know the internal working of the phone such as how the circuits are connected and blah blah. You have very user friendly interface given to use them. For example you have buttons given on the phone and there are numbers. You just type them and internal controller will do its job to handle your request. So what is done here is that few of the things are there doing their job internally but you don't know its operation or rather you don't care. 



  • We have achieved Information hiding here by not showing internal working. 
We use the mobile interface to operate it and as a result we don't care about the internal things i.e. we have suppress complex things.



  • Encapsulation is a way to obtain "information hiding" so, you don't "need to know the internal working of the mobile phone to operate" with it. You have an interface to use the device without knowing implementation details.



  • Abstraction is a technique for managing complexity of computer systems. Abstraction captures only those details about an entity that are relevant to the current perspective. We achieve abstraction by using encapsulation i.e. information is hidden by Encapsulation.
So for above example internal phone circuits are hidden and we have interface to use the mobile and hence we have achieved abstraction here.

Encapsulation and Abstraction in program:

class A
    {
        private int a;
        public void print()
        {
            Console.WriteLine("Value of a = {0}", a);
        }
    }
    class B
    {
        static void Main(string[] args)
        {
            A a = new A();
            a.print();                
        }
    }
  • In above program class A has a variable 'a' which is declared as private that means class B can not see the variable 'a'. If we want to use 'a' then we have to use class A's method print() which internally uses the same.
Hence we made 'a' hidden and have achieved abstraction by doing so.

0 comments:

Thanks for your Feedback !!!

Fundamentals of OOP - Classes and Objects




  • As the name suggest Object oriented programming is based on "objects" which is nothing but an instance of a class. Class contains fields often called attributes and blocks of code in the form of methods. So we can say object comprises of attributes and methods. In simple word, class is a blueprint for object and defines structure of object.
  • By creating classes we categorize the data we are representing and give these representations a context by associating them with features in the programming languages API. However a class is never used in the main program, it must first be instantiated as an object and the object is what we would use in our main program. In this sense a class is more like a blueprint, that describes the complex relationships between the data we are representing, like the blueprint of a house can describe how tall a wall will be or how far a window will be from the ceiling. If you can imagine a class to be like a blueprint then a software object is like a house made from that blueprint. For example, a class only describes the possible objects that can be instantiated from it, taking the blueprint analogy further we cannot live in a blueprint yet it has all the information we need to build a house which would be something that we use as a functional object. In other words we use the blueprint to build a house, this is just like instantiating an object from a class. The class only describes the possibilities of an object that can be instantiated from it and when we finally do instantiate an object from the class, it is the object that we use in our main program.
  • Real world example:

See the following image where we can see, the blueprint defines the structure of house which will help us construct the house. We can build as many houses(Objects) with different properties(fields) as we want just by changing the values.


Blueprint example

Note(image taken from lyndondaniels.com)


0 comments:

Thanks for your Feedback !!!