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:
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 !!!