In an earlier post, we discussed introduction to OOP and we saw the fundamentals of Object Oriented Programming. Today we are going to see those fundamentals in detail.
The first and for most is Encapsulation. Encapsulation provides a way to wrap or bundle your object thereby allowing you to protect your object data and behavior from external world.
"Encapsulation packages the data and method of an object and provides protection from external tampering by users. It implies that there is visibility to the functionalities offered by an object, and no visibility to its data. The best application of encapsulation is making the data fields private and using public accessor methods." - IDYNSolutions
Consider the same example again,
public class Monitor
{
private bool isOn;
private string companyName;
public void TurnOn()
{
isOn = true;
Display();
}
public void TurnOff()
{
isOn = false;
}
public void Display()
{
//...
}
}
The Monitor class hides its internal attributes, say isOn, to the external world and it encapsulates them and provide their accessor though public methods TurnOn and TurnOff. The private keyword does not allow access outside the class. For instance, consider the code snippet below:
Monitor dellMonitor=new Monitor();
dellMonitor.isOn= false; // compile time error, private field cannot be accessed outside class
dellMonitor.TurnOn(); //Compiles correctly, its public and hence can be accessed from outside
The second pillar is Inheritance. Object Oriented Programming allows relationship among classes. It allows classes to inherit commonly used state and behavior from other classes. A class can be parent of another class. For instance,
public class Person
{
protected string name;
protected int id;
public void SetID(int personID)
{
id = personID;
}
}
public class Programmer : Person
{
public void SetName(string programmerName)
{
base.name = programmerName; //base keyword is a reference to parent class
}
}
Here class Programmer is child class of Person or class Person is parent of Programmer class. The relationship is Every Programmer is also a Person :) isn't it? ;).
The access modifier 'protected' means that the members are accessible within class and in its child class. Programmer class will inherit the protected and public members of Parent class here Person. The name and id in class Person are also accessible in Programmer class. Now consider the code snippet
Programmer dotnetPro=new Programmer();
dotnetPro.SetName("Adil");
dotnetPro.SetID(101); //As child it can access parent's method SetID
In short, Inheritance allows you to define relationship among classes and inherit common states and behavior among them. Reusability is one of the major advantage of inheritance.
The last pillar of OOP is Polymorphism. Polymorphism is not a very simple concept, there are thousands of articles written on it. I would recommend you to visit Polymorphism (C# Programming Guide) and Wikipedia.
There are lots of article on the www. I just wrote brief summary on these topics. Also, Pillars of OOP are one of the most common interview questions so give them some time and read them in detail.
Till next post Good bye.