What is Encapsulation?
What is Abstraction?
What is the functionality of Encapsulation and Abstraction in C#?
Encapsulation and abstraction is essential part of C sharp programming that is mostly used for hide complex code from unauthorized user and shows only relevant information.
What is encapsulation?
Encapsulation is the process of hiding irrelevant data from the user. To understand encapsulation, consider an example of mobile phone. Whenever you buy a mobile, you don’t see how circuit board works. You are also not interested to know how digital signal converts into analog signal and vice versa. These are the irrelevant information for the mobile user, that’s why it is encapsulated inside a cabinet.
In C# programming, we will do same thing. We will create a cabinet and keep all the irrelevant information in it that will be unavailable for the user.
What is abstraction?
Abstraction is just opposite of Encapsulation. Abstraction is mechanism to show only relevant data to the user. Consider the same mobile example again. Whenever you buy a mobile phone, you see their different types of functionalities as camera, mp3 player, calling function, recording function, multimedia etc. It is abstraction, because you are seeing only relevant information instead of their internal engineering.
What is Polymorphism?
Polymorphism means one name many forms. Polymorphism means one object behaving as multiple forms.
Ex: Person behaves as a SON in house, at the same time behaves like an EMPLOYEE in the office.
Overloading
namespace MethodOverloadingByManishAgrahari
{
class Program
{
public class TestOverloading
{
public void Add(string a1, string a2)
{
Console.WriteLine("Adding Two String :" + a1 + a2);
}
public void Add(int a1, int a2)
{
Console.WriteLine("Adding Two Integer :" + a1 + a2);
}
}
static void Main(string[] args)
{
TestOverloading obj = new TestOverloading();
obj.Add("Manish " , "Agrahari");
obj.Add(5, 10);
}
}
}
Overriding
By runtime polymorphism, we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding or Dynamic binding.
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{
public virtual void Show()
{
Console.WriteLine("Show From Base Class.");
}
public class Derived : Base
}
{
public override void Show()
{
Console.WriteLine("Show From Derived Class.");
}
}
static void Main(string[] args)
{
Base objBase;
objBase = new Base();
objBase.Show();// Output ----> Show From Base Class.
objBase = new Derived();
objBase.Show();//Output--> Show From Derived Class.
}
}
}
Virtual Keyword
“The
virtual
keyword is used to modify/override a base class method in a derived class.”
Method Overriding Vs Method Hiding
Method overriding allows a subclass to provide a specific implementation of a method that is already provided by base class. The implementation in the subclass overrides (replaces) the implementation in the base class.
Method hiding does not have a relationship between the methods in the base class and derived class. The method in the derived class hides the method in the base class.
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{
public virtual void Show()
{
Console.WriteLine("Show From Base Class.");
}
}
public class Derived : Base
{
public new void Show()
{
Console.WriteLine("Show From Derived Class.");
}
}
static void Main(string[] args)
{
Base objBaseRefToDerived = new Derived();
objBaseRefToDerived.Show();//Output--> Show From Base Class.
}
}
}
Sealed Keyword
Sealed
keyword can be used to stop method overriding in a derived classes.
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{
public virtual void Show()
{
Console.WriteLine("This is Base Class.");
}
}
public class Derived : Base
{
public override sealed void Show()
{
Console.WriteLine("This is Derived Class.");
}
}
static void Main(string[] args)
{
Base objBaseReference = new Derived();
objBaseReference.Show();// Output ---> This is Derived Class.
Console.ReadLine();
}
}
}
Output ---> This is Derived Class.
Yield keyword
Yield keyword helps us to do custom stateful iteration over .NET collections.”
There are two scenarios where “yield” keyword is useful:-
- Customized iteration through a collection without creating a temporary collection.
- Stateful iteration(ex: Runing total)
- Scenario 1:- Customized iteration through a collectionLet’s try to understand what customized iteration means with an example. Consider the below code.Let say we have a simple list called as “MyList” which has collection of 5 continuous numeric values 1,2,3,4 and 5. This list is browsed/iterated from console application from within static void main method.For now let’s visualize the “main()” method as a caller. So the caller i.e. “main()” method calls the list and displays the items inside it. Simple…till now
".
static List<int> MyList = new List<int>(); static void FillValues() { MyList.Add(1); MyList.Add(2); MyList.Add(3); MyList.Add(4); MyList.Add(5); } static void Main(string[] args) // Caller { FillValues(); // Fills the list with 5 values foreach (int i in MyList) // Browses through the list { Console.WriteLine(i); } Console.ReadLine(); }
Now let me complicate this situation let’s say the caller only wants values greater than “3” from the collection. So the obvious thing as a c# developer we will do is create a function as shown below. This function will have temporary collection. In this temporary collection we will first add values which are greater than “3” and return the same to the caller. The caller can then iterate through this collection.
static IEnumerable<int> FilterWithoutYield() { List<int> temp = new List<int>(); foreach (int i in MyList) { if (i > 3) { temp.Add(i); } } return temp; }
Now the above approach is fine but it would be great if we would get rid of the collection, so that our code becomes simple. This where “yield” keyword comes to help. Below is a simple code how we have used yield.
“Yield” keyword will return back the control to the caller, the caller will do his work and re-enter the function from where he had left and continue iteration from that point onwards. In other words “yield” keyword moves control of the program to and fro between caller and the collection.
static IEnumerable<int> FilterWithYield() { foreach (int i in MyList) { if (i > 3) yield return i; } }
So for the above code following are details steps how the control will flow between caller and collection. You can also see the pictorial representation in the next diagram shown below.
- Step 1:- Caller calls the function to iterate for number’s greater than 3.
- Step 2:- Inside the function the for loop runs from 1 to 2 , from 2 to 3 until it encounters value greater than “3” i.e. “4”. As soon as the condition of value greater than 3 is met the “yield” keyword sends this data back to the caller.
- Step 3:- Caller displays the value on the console and re-enters the function for more data. This time when it reenters, it does not start from first. It remembers the state and starts from “5”. The iteration continues further as usual.