Three Pillars C#
Three Pillars C#
1. Encapsulation
2. Inheritance
3. Polymorphism
Abstraction
Abstraction is "To represent the essential feature without representing the
background details."
Abstraction lets you focus on what the object does instead of how it does it.
Abstraction is the process of hiding the working style of an object, and showing the
information of an object in an understandable manner.
Abstract information (necessary and common information) for the object "Mobile
Phone" is that it makes a call to any number and can send SMS.
So that, for a mobile phone object you will have the abstract class as in the
following,
Abstraction means putting all the variables and methods in a class that are
necessary.
Example
If somebody in your college tells you to fill in an application form, you will provide
your details, like name, address, date of birth, which semester, percentage you
have etcetera.
If some doctor gives you an application to fill in the details, you will provide the
details, like name, address, date of birth, blood group, height and weight.
Age, name and address, so you can create a class that consists of the common
data. That is called an abstract class.
Encapsulation
Wrapping up a data member and a method together into a single unit (in other
words class) is called Encapsulation.
Encapsulation is like enclosing in a capsule. That is enclosing the related operations
and data related to an object into that object.
Encapsulation is like your bag in which you can keep your pen, book etcetera. It
means this is the property of encapsulating members and functions.
1. class Bag {
2. book;
3. pen;
4. ReadBook();
5. }
Encapsulation means hiding the internal details of an object, in other words how an
object does something.
Encapsulation prevents clients from seeing its inside view, where the behaviour of
the abstraction is implemented.
Hide the data for security such as making the variables private, and expose the
property to access the private data that will be public.
So, when you access the property you can validate the data and set it.
Example 1
1. class Demo {
2. private int _mark;
3. public int Mark {
4. get {
5. return _mark;
6. }
7. set {
8. if (_mark > 0) _mark = value;
9. else _mark = 0;
10. }
11. }
12. }
Suppose you are a Mobile Phone Manufacturer and you have designed and
developed a Mobile Phone design (a class). Now by using machinery you are
manufacturing Mobile Phones (objects) for selling, when you sell your Mobile
Phone the user only learns how to use the Mobile Phone but not how the Mobile
Phone works.
This means that you are creating the class with functions and by with objects
(capsules) of which you are making available the functionality of your class by that
object and without the interference in the original class.
Example 2
TV operation
It is encapsulated with a cover and we can operate it with a remote and there is no
need to open the TV to change the channel.
Here everything is private except the remote, so that anyone can access the remote
to operate and change the things in the TV.
Real-world Example
You have a Mobile Phone, you can dial a number using keypad buttons. You don't
even know how these are working internally. This is called Abstraction. You only
have the information that is necessary to dial a number. But not internal working of
the mobile.
But how does the Mobile Phone work internally? How are the keypad buttons
connected with internal circuit? That is called Encapsulation.
Inheritance
When a class includes a property of another class it is known as inheritance.
Output
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Polymorphism
Polymorphism means one name, many forms.
Example 1
Example 2
A person behaves the son in a house at the same time that the person behaves an
employee in an office.
Example 3
As phone
As camera
As mp3 player
As radio
Types of Polymorphism
There are two types of polymorphism in C#:
Here C# compiler checks the number of parameters passed and the type of
parameter and make the decision of which method to call and it throw an error if
no matching method is found.
In the following example, a class has two methods with the same name "Add" but
with different input parameters (the first method has three parameters and the
second method has two parameters).
A method or function of the base class is available to the child (derived) class
without the use of the "overriding" keyword. The compiler hides the function or
method of the base class. This concept is known as shadowing or method hiding.
You may find the difference between overriding and shadowing here.
1. public class X
2. {
3. public virtual void A()
4. {
5. }
6. }
7. public class Y : X
8. {
9. public override void A()
10. {
11. }
12. }
1. public class Y : X
2. {
3. public sealed override void A()
4. {
5. }
6. }
1. public class X
2. {
3. public virtual void A()
4. {
5. }
6. }
7. public class Y : X
8. {
9. public override void A()
10. {
11. base.A();
12. }
13. }