OOP stand for object oriented Programming. OOP embed
real life examples, it base on two fundamentals concepts, data and the way to
manipulate that data.
Pascal, C used the procedural approach that focuses on
the manipulation of data not the data itself. Procedural approach has many
drawbacks such as re-usability issues, and lack of maintainability.
OOP solves these issues smartly.
Pillars of OOP
There are four basic pillars of OOP. Namely
- Object 2. Class 3. Inheritance 4. Polymorphism
OBJECT
What is object?
Object can be anything like laptop, marker, car, human
being.
Object has two things
- State
- Behavior
For example marker
State
|
behavior
|
Length
|
Write
|
Width
|
Draw
|
Color
|
But according to Object Oriented Programming
State /property à
Attribute
Behavior à
Method
Now we will use Attribute word instead of State
/property and Method word instead of Behavior in our lecture.
For example: Let’s talk about Bank Account.
Attributes(State)
|
Methods(behavior)
|
CustomerID
|
Deposit
|
AccountNo
|
Withdraw
|
Name
|
:
|
Address
|
:
|
Balance
|
etc
|
CLASS
A class is a blueprint/template from which individual
objects are created.
Class is a collection of attributes and methods.
Blueprint à a
detailed outline or plain of action.
Understanding blueprint/template.
Consider a table below:
Object
1
|
Object
2
|
Object
3
|
Attributes
|
||
Make=“Honda”
|
Make=“Toyota”
|
Make=“Suzuki”
|
Model=2012
|
Model=2014
|
Model =2011
|
Owner=”
Ahmed”
|
Owner=” Saeed”
|
Owner=” saleh”
|
Color=”White”
|
Color=”Black”
|
Color=”Blue”
|
Methods
|
||
Accelerate()
|
Accelerate()
|
Accelerate()
|
Decelerate()
|
Decelerate()
|
Decelerate()
|
As you can see there are three objects with same
attributes and methods name. For above table we can create a class like this
Class
(you can say Car Class)
|
Make
Model
Owner
Color
|
Accelerate
Decelerate
|
Here we can say that we created a template/blueprint of
object.
Let’s talk about object 1
We simply use template (class) as we defined previously
by giving values to attribute, and calling method.
Make=”Honda”
Model=2012
Owner=”Ahmed”
Accelerate()
Decelerate()
Programming
a Class
In C# class is created by keyword ‘class’.
Class can be created in two deferent way, inside
Program.cs file or can create separately. We will see both ways.
In below figure I want to create separate class.
Go to solution explorer then click on project name and add
Class
It will asked class name
New class will be open like this.
Let’s some coding that we have seen in our above
example.
Yes we have created template/blueprint (class), now
this is the time to create the objects of class like object1, object2, object3
as we have seen before in our example.
Go to program.cs class and create object.
What does?
Car object1 = new
Car(); means.
This is
similar to
int a=10; here I declare and initialize the variable of type
integer.
But an object is instantiated using the new keyword, the JIT compiler
(Just in time compiler) allocate the memory for the object and return the
reference of that allocated memory.
How
to use attribute and method of the class by the object1?
Simply write object1 and press ‘.’ .Then you can see
all the attribute and method of that class, as shown in figure below.
Coding for Object1, Object2, Object3
Car object1 = new Car();
object1.Make = "Honda";
object1.Model = 2012;
object1.Owner = "Ahmed";
object1.color = "White";
Console.WriteLine("Car
Make = {0}", object1.Make);
Console.WriteLine("Car
Model = {0}", object1.Model);
Console.WriteLine("Car
Owner = {0}", object1.Owner);
Console.WriteLine("Car
color = {0}", object1.color);
object1.Accelerate();
object1.Decelerate();
Car object2 = new Car();
object2.Make = "Toyota";
object2.Model = 2014;
object2.Owner = "Saeed";
object2.color = "Black";
Console.WriteLine("Car
Make = {0}", object2.Make);
Console.WriteLine("Car
Model = {0}", object2.Model);
Console.WriteLine("Car
Owner = {0}", object2.Owner);
Console.WriteLine("Car
color = {0}", object2.color);
object2.Accelerate();
object2.Decelerate();
Car object3 = new Car();
object3.Make = "Suzuki";
object3.Model = 2011;
object3.Owner = "Saleh";
object3.color = "Blue";
Console.WriteLine("Car
Make = {0}", object3.Make);
Console.WriteLine("Car
Model = {0}", object3.Model);
Console.WriteLine("Car
Owner = {0}", object3.Owner);
Console.WriteLine("Car
color = {0}", object3.color);
object3.Accelerate();
object3.Decelerate();
Output:
As you can see we created three object using a single
class it means that class provide us reusability of attribute and method, this
was the example of 3 objects but we can make thousands of object for using
attributes and methods of single class.
You can also create inside Program.cs which is not good
way to create class.
Like
Inheritance
The creation of new class based on the attribute, property and methods of existing class. The created class is derived class or child class and the existing class is based or parent class.
It is the backbone of the OOP which allow reusability of inherited attributes method etc.
Polymorphism
Poly means many and morphs means faces, it is the ability to behave differently in different situation. It is basically seen in program where you have multiple methods declared with the same name but with different parameters and different behaviour or methods declared with the same name but using virtual and override keyword with method name.
but there are also two basic features of Object Oriented Programming but not pillars.
- abstraction
- Encapsulation
Abstraction
Abstraction is defined as, extracting only required information from the object. For example Laptop is an object, only manual is given to the user for operating the Laptop (like port details, charging and discharging of battery time etc.), not given any technical details, thus giving only an abstraction to the user.
Encapsulation
Class only allow specific information to other class and hidden the other information. Like methods and attributes. Encapsulation is implemented by using access modifiers. An access modifier defines the scope and visibility of a class member. C# supports the following access modifiers:
Public, Private, Protected, Internal, Protected internal
Later we will see Access Modifiers.
No comments:
Post a Comment