0% found this document useful (0 votes)
131 views13 pages

Three Pillars C#

The document discusses three main concepts of object-oriented programming in C#: encapsulation, inheritance, and polymorphism. It provides examples and definitions of each concept. Abstraction, encapsulation, inheritance, and polymorphism are key pillars of OOP that allow for code reuse and organization. Real-world examples like mobile phones are used to illustrate the concepts.

Uploaded by

Padmashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views13 pages

Three Pillars C#

The document discusses three main concepts of object-oriented programming in C#: encapsulation, inheritance, and polymorphism. It provides examples and definitions of each concept. Abstraction, encapsulation, inheritance, and polymorphism are key pillars of OOP that allow for code reuse and organization. Real-world examples like mobile phones are used to illustrate the concepts.

Uploaded by

Padmashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Three pills of OOP with C#

All the programming languages supporting Object Oriented Programming will be


supporting these three main concepts,

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 provides you a generalized view of your classes or objects by providing


relevant information.

Abstraction is the process of hiding the working style of an object, and showing the
information of an object in an understandable manner.

Real-world Example of Abstraction

Suppose you have an object Mobile Phone.

Suppose you have 3 mobile phones as in the following:

Nokia 1400 (Features: Calling, SMS)


Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording,
Reading E-mails)

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,

1. abstract class MobilePhone {


2. public void Calling();
3. public void SendSMS();
4. }
5. public class Nokia1400: MobilePhone {}
6. public class Nokia2700: MobilePhone {
7. public void FMRadio();
8. public void MP3();
9. public void Camera();
10. }
11. public class BlackBerry: MobilePhone {
12. public void FMRadio();
13. public void MP3();
14. public void Camera();
15. public void Recording();
16. public void ReadAndSendEmails();
17. }

Abstraction means putting all the variables and methods in a class that are
necessary.

For example: Abstract class and abstract method.

Abstraction is a common thing.

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.

See in the preceding example what is in common?

Age, name and address, so you can create a class that consists of the common
data. That is called an abstract class.

That class is not complete and it can be inherited by other classes.

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.

Encapsulation is a technique used to protect the information in an object from


another object.

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. }

Real-world Example of Encapsulation


Let's use as an example Mobile Phones and Mobile Phone Manufacturers.

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.

Differences between Abstraction and Encapsulation


Abstraction Encapsulation
1. Abstraction solves the problem 1. Encapsulation solves the problem in the
at the design level. implementation level.
2. Encapsulation means hiding the code and data
2. Abstraction hides unwanted
into a single unit to protect the data from the
data and provides relevant data.
outside world.
3. Abstraction lets you focus on 3. Encapsulation means hiding the internal
what the object does instead of details or mechanics of how an object does
how it does it something.
4. Abstraction: Outer layout, used
4. Encapsulation- Inner layout, used in terms of
in terms of design.
implementation.
For example:
An external of a Mobile Phone, For example: the internal details of a Mobile
like it has a display screen and Phone, how the keypad button and display
keypad buttons to dial a number. screen are connected with each other using
circuits.

The easier way to understand abstraction and encapsulation is as follows.

Real-world Example

Use an example of a Mobile Phone

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.

Inheritance is a process of object reusability.

For example, a child includes the properties of its parents.

1. public class ParentClass {


2. public ParentClass() {
3. Console.WriteLine("Parent Constructor.");
4. }
5. public void print() {
6. Console.WriteLine("I'm a Parent Class.");
7. }
8. }
9. public class ChildClass: ParentClass {
10. public ChildClass() {
11. Console.WriteLine("Child Constructor.");
12. }
13. public static void Main() {
14. ChildClass child = new ChildClass();
15. child.print();
16. }
17. }

Output

Parent Constructor.
Child Constructor.
I'm a Parent Class.

Polymorphism
Polymorphism means one name, many forms.

One function behaves in different forms.

In other words, "Many forms of a single object is called Polymorphism."

Real-world Example of Polymorphism

Example 1

A teacher behaves students.

A teacher behaves his/her seniors.

Here teacher is an object but the attitude is different in different situations.

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

Your mobile phone, one name but many forms:

 As phone
 As camera
 As mp3 player
 As radio

Types of Polymorphism
There are two types of polymorphism in C#:

 Static / Compile Time Polymorphism.


 Dynamic / Runtime Polymorphism.

Static or Compile Time Polymorphism


It is also known as Early Binding. Method overloading is an example of Static
Polymorphism. In overloading, the method / function has a same name but
different signatures. It is also known as Compile Time Polymorphism because the
decision of which method is to be called is made at compile time. Overloading is the
concept in which method names are the same with a different set of parameters.

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).

1. public class TestData


2. {
3. public int Add(int a, int b, int c)
4. {
5. return a + b + c;
6. }
7. public int Add(int a, int b)
8. {
9. return a + b;
10. }
11. }
12. class Program
13. {
14. static void Main(string[] args)
15. {
16. TestData dataClass = new TestData();
17. int add2 = dataClass.Add(45, 34, 67);
18. int add1 = dataClass.Add(23, 34);
19. }
20. }

Dynamic / Runtime Polymorphism


Dynamic / runtime polymorphism is also known as late binding. Here, the method
name and the method signature (number of parameters and parameter type must
be the same and may have a different implementation). Method overriding is an
example of dynamic polymorphism.

Method overriding can be done using inheritance. With method overriding it is


possible for the base class and derived class to have the same method name and
same something. The compiler would not be aware of the method available for
overriding the functionality, so the compiler does not throw an error at compile
time. The compiler will decide which method to call at runtime and if no method is
found then it throws an error.

1. public class Drawing


2. {
3. public virtual double Area()
4. {
5. return 0;
6. }
7. }
8.
9. public class Circle : Drawing
10. {
11. public double Radius { get; set; }
12. public Circle()
13. {
14. Radius = 5;
15. }
16. public override double Area()
17. {
18. return (3.14) * Math.Pow(Radius, 2);
19. }
20. }
21.
22. public class Square : Drawing
23. {
24. public double Length { get; set; }
25. public Square()
26. {
27. Length = 6;
28. }
29. public override double Area()
30. {
31. return Math.Pow(Length, 2);
32. }
33. }
34.
35. public class Rectangle : Drawing
36. {
37. public double Height { get; set; }
38. public double Width { get; set; }
39. public Rectangle()
40. {
41. Height = 5.3;
42. Width = 3.4;
43. }
44. public override double Area()
45. {
46. return Height * Width;
47. }
48. }
49.
50. class Program
51. {
52. static void Main(string[] args)
53. {
54.
55. Drawing circle = new Circle();
56. Console.WriteLine("Area :" + circle.Area());
57.
58. Drawing square = new Square();
59. Console.WriteLine("Area :" + square.Area());
60.
61. Drawing rectangle = new Rectangle();
62. Console.WriteLine("Area :" + rectangle.Area());
63. }
64. }
The compiler requires an Area() method and it compiles successfully but the right
version of the Area() method is not being determined at compile time but
determined at runtime. Finally the overriding methods must have the same name
and signature (number of parameters and type), as the virtual or abstract method
defined in the base class method and that it is overriding in the derived class.

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.

Preventing Derived class from overriding virtual members


Virtual members remain “virtual” indefinitely. In other words, virtual members
remain “virtual” regardless of how many classes have been between virtual
members and the class that originally declared it. For example, if class X has the
virtual method "A" and the class Y is derived from X and the class Z is derived from
Y, class Z inherits the virtual method "A" and override it.

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. }

A derived class is able to stop virtual inheritance by declaring an override member


as "sealed".

1. public class Y : X
2. {
3. public sealed override void A()
4. {
5. }
6. }

Accessing Base class virtual member


Using the "base" keyword, the derived class is able to access the method.

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. }

You might also like