Pillars of Object Oriented Programming

There are 4 pillars of Object Oriented Programming:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Abstraction:

Abstraction is the process of hiding implementation details and showing only the necessary part to the outside world.
Example:
Sending an email is one of the best example of Abstraction. While sending email, we write the email body, subject and provide recipient address and simple send email but we don’t know the implementation details of backend functionality.

Encapsulation:

Inheritance is the process of adopting the features of Parent(Base) class. Inheritance allows the child class to acquire properties and functionality of Parent class. If a child class inherits from a parent class then child class will have it own as well as parent class members and functions.

Polymorphism:

We can divide the word Polymorphism in two pieces “Poly” which means “multiple” and “morphism” which means “forms”. In OOP the word Polymorphism refers to multiple forms of an object.
There are two types of Polymorphism in OOP:

  1. Method Overloading
  2. Method Overriding

1-Method Overloading:

Method Overloading is also called Compile time Polymorphism. Method Overloading is the concept of having different functions/methods with same name but different parameters. Note that Overloading is never done by method return type.

Example:

public class Shape
	   {	       
		   public void DrawSquare(int width,int height)
		   {
		      //Implementation
		   }
		   
		   
		   public void DrawSquare(int x1,int y1, int x2, int y2)
		   {
		      //Implementation
		   }
	   }

2-Method Overriding:

Method Overriding is also called Runtime Polymorphism. Method overriding is the process of overriding the base class methods in to the child classes.

Example:
In this example, Circle class will override the functionality of Shape Class Draw() function with in Circle class.

public class Shape
	   {	       
		   public virtual void Draw(int width,int height)
		   {
		      //Implementation
		   }
	   }
	   
	   Public class Circle : Shape
	   {
	       public override void Draw(int width,int height)
		   {
		      //Override Implementation
		   }
	   }