Search Placementyogi.com
 

Pillars of Object Oriented Programming in C#.NET

Pillars of Object Oriented Programming in C#

There are 3 pillars of Object Oriented Programming:

  1.  Encapsulation
  2.  Inheritance
  3.  Polymorphism
 

But before we start mentioning the pillars of OOPS you should be aware of OOPS related terms like class, objects etc.

  1. Class : A Class is like a mould and defines the structure and behavior of objects.
  2. Object: An Object is an instance of a Class.
  3. Member Variables: The variables defined in a Class.(DATA)
  4. Member Methods: The methods defined inside a Class. (CODE)
  5. Public Members: A Method or Instance variable that can be accessed by code not belonging to the object.
  6. Private Members: A Method or Instance variable that can be accessed only by the members of a Class.

 

(OPTIONAL) If you like to understand using a program go through the following Simple Program. (You can skip this program if you want to)

class A
{
	private int my1stMemberVariable; 
	private int my2ndMemberVariable;
	int my3rdMemberVariable;
	// this method will always return the current value of my1stMemberVariable
	public int getMy1stMemberVariable()
	{
		return my1stMemberVariable;
	}
	
	// this method can only change the value of my1stMemberVariable
	public void setMy1stMemberVariable(int val) 
	{
		Console.WriteLine("setMy1stMemberVariable(int val) Called");
		Console.WriteLine("setting my1stMemberVariable to"+ val );
		my1stMemberVariable=val;
	}
	public void setMy1stMemberVariable(double val)
	{
		Console.WriteLine("setMy1stMemberVariable(double val) Called");
		Console.WriteLine("setting my1stMemberVariable to"+ val );
		my1stMemberVariable=(int)val; // typecase double val to int
	}
	// this method will always return the current value of my1stMemberVariable
	public int getMy2ndMemberVariable()
	{
		return my2ndMemberVariable;
	}
	
	// this method can only change the value of my1stMemberVariable
	public void setMy2ndMemberVariable(int val)
	{
		 my2ndMemberVariable=val;
	
	}
	public static void main(String[] args)
	{
		A myObj=new A();
		//myObj.my1stMemberVariable =10;
                //Not Allowed to access private instance variables
		Console.WriteLine("myObj.my1stMemberVariable: "+myObj.my1stMemberVariable);
		myObj.setMy1stMemberVariable(10);// Allowed to access public methods which
		// which access private instance variables !

		Console.WriteLine("myObj.my1stMemberVariable: "+myObj.my1stMemberVariable);
		myObj.setMy1stMemberVariable(20.6);
		Console.WriteLine("myObj.my1stMemberVariable: "+myObj.my1stMemberVariable);
		Console.WriteLine("myObj.my3rdMemberVariable: "+myObj.my3rdMemberVariable);
		Console.WriteLine("setting my3rdMemberVariable to 20" );
		myObj.my3rdMemberVariable=20; // Allowed to access public intance variables
		Console.WriteLine("myObj.my3rdMemberVariable: "+myObj.my3rdMemberVariable);
		// Statements 
	}
}
/*

Output:

myObj.my1stMemberVariable: 0

setMy1stMemberVariable(int val) Called

setting my1stMemberVariable to10

myObj.my1stMemberVariable: 10

setMy1stMemberVariable(double val) Called

setting my1stMemberVariable to20.6

myObj.my1stMemberVariable: 20

myObj.my3rdMemberVariable: 0

setting my3rdMemberVariable to 20

myObj.my3rdMemberVariable: 20

*/

 

1.  Encapsulation:

Encapsulation is the mechanism of binding together the data and the code, so that they are not misused or accidentally modified.

Any application that has to access the data has to call the appropriate code.

 Encapsulation in C#

Consider an Analogy here to the real world:

Medicine store example to explain Encapsulation:

Lets say you have to buy some medicines. You go to the medical store and ask the chemist for the meds. Only the chemist has access to the medicines in the store based on your prescription. The chemist knows what medicines to give to you. This reduces the risk of you taking any medicine that is not intended for you. 

In this example,

MEDICINES == Member Variables.

CHEMIST   == Member Methods.

You       == External Application or piece of Code.

 

So, If Any external Application has to access the Member Variables It has to call the appropriate Member Methods which will do the task for it.(If You have to access the Medicines You have to ask the Chemist). This way the member variables are secure and encapsulated by a layer of Member Methods.

The Member Methods and Member Variables are bundled together in to objects and can be accessed only by the objects.

So you need 2 steps if you have to access a public member of a class you have to:

  1.  Create an object of the class
  2.  Then access the member through object.

You need 3 steps if you want to access the private members of a class

  1.  You have to create an object of the class 
  2.  Then access the public method of the class through the object
  3.  Then access the private member of the class through the public method which has access to it.

Also, encapsulation ensures that you do not accidentally modify something else. i.e. if you call the method setMy1stMemberVariable() it modifies only my1stMemberVariable and does not changes my2ndMemberVariable i.e. there are no side effects!

Now refer to the above program and read the comments. You should understand it properly.

 

2. Inheritance

Inheritance is the process by which one object acquires the properties of another object

Just like a baby acquires the properties of his/her parents like hair color, hair pattern, height, skin color etc. in addition to his/her own properties.

To be more generic, A human can not give birth to a puppy :-) Coz there are certain properties that has to be inherited, no matter what.

In terms of Programming , An Object can inherit properties from another class.

 Inheritance in C#

A Class "Villa" or "Apartment" are basically a type of Building.

So, Building is the parent class (Superclass) of both "Villa" and "Apartment" (SubClasses) , properties like numberOfFloors is a common property of class "Building" that both the SubClasses will inherit.

This is like a Hierarchy Hence is called Hierarchical Inheritance.

The different types of Inheritance are:

  1.  Single Inheritance
  2.  Hierarchical Inheritance
  3.  Multi Level Inheritance
  4. Hybrid Inheritance

We will explain all of the above in detail later when we cover Inheritance.

3. Polymorphism

Polymorphism is the process of exhibiting multiple actions with only one interface. In fact everything around us exhibit polymorphism in one way or another.

The best example is the GOOGLE Search Engine. There is just one search Textbox which you can use to search for everything. You can search for "Wolverine 2013 movie" or "Objective Questions on C#" and it would present the related pages. There is NO SEPARATE SEARCH BOX for each category and thus "One Interface multiple Actions"!! Nailed it!!

In the program given at the top.

There are two methods to set the my1stMemberVariable.

setMy1stMemberVariable(int val) // accepts int
setMy1stMemberVariable(double val) // accepts double

These are 2 methods but have the same name. The compiler automatically chooses the correct method to execute based on the type of argument you pass.

calling setMy1stMemberVariable(10) calls the first method and calling setMy1stMemberVariable(10.6) calls the second method.

 

Abstraction:

Abstraction is the process of hiding unnecessary details from the user to avoid complexity and distraction. It is one of the important properties of OOPS and is sometimes even considered the 4th Pillar of Object Oriented Programming System.

When you press a key on your keyboard the character appears on the screen, you need to know only this, but How exactly it works based on the electronically is not needed. This is called Abstraction.

Another Example is when you use the remote control of your TV, you do not bother about how pressing a key in the remote changes the channel on the TV. You just know that pressing the + volume  button will increase the volume!

In the above program, when you call the method setMy1stMemberVariable() you do not need to bother about the actual implementation of the method, you just want the variable my1stMemberVariable to be changed.

Banner to LogicGuns.com

Hide Page Information

Tutorial - Pillars of Object-Oriented Programming in C# | Encapsulation | Inheritance | Polymorphism

Tutorial - Pillars of Object-Oriented Programming in C# | Encapsulation | Inheritance | Polymorphism

Here you can find complete tutorial on Encapsulation, Inheritance, Polymorphism, Abstraction through easy learning with diagrams.

Why Pillars of Object-Oriented Programming in C#?

In this section you can learn about Encapsulation, Inheritance, Polymorphism, Abstraction in C# to improve your .NET skills in order to face the interviews, competitive examinations and various entrance tests with full confidence.

Where can I get tutorials Encapsulation, Inheritance, Polymorphism, Abstraction in C#?

PlacementYogi provides tutorial on Encapsulation, Inheritance, Polymorphism, Abstraction through easy learning process focusing interviews.

You can easily learn Encapsulation, Inheritance, Polymorphism, Abstraction through our compact and best tutorials.