Introduction:
Ø
OOP stands for Object Oriented Programming
Ø
It is programming methodology that uses Object
to build a system or web applications using programming language like C#, Java,
etc……
Ø
Here Object plays a very important rule because
it hides the implementation details and exposed only the needed functionality
and related stuff that is required to adopt it.
Ø
We can access class properties and methods by
creating class object.
Class:
Ø
A class is a collection of object
Ø
A class is a blueprint / skeleton / plan / template of an object
Ø
It contains properties
(attributes), methods (behaviours), constructor, destructor etc….
Ø
Basically it’s an reference type, it take heap memory to store the information but
it’s allow user make own class, that’s why we also called as “user defined data type”
1 2 3 4 | public class Bike { //your code goes here.. } |
Method:
Ø
Method is an object’s behaviour
Ø
Method are functions defined inside the body of
a class
Ø
They are used to perform some operation with
help of attributes of our objects. Methods brings modularity to our programs.
For Instance, if you consider
“Bike” as a class then its behaviour is
to get bike color, engine, mileage etc…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Bike { //here is some properties of class Bike public string color; public string engine; public int mileage; //here is some behavior/methods of class Bike public string GetColor() { return "red"; } public int GetMileage() { return 65; } } |
In above example GetColor() and
GetMileage() are methods consider as a object’s behaviour
Object:
Ø
An Object is a real-world entity that keeps
together property states and behaviours.
Ø
Object is the basic unit of object-oriented programming
Ø
Objects are identified by its unique name
Ø
Every object differ from other objects either by
state or behaviour
Ø
A third party object can be called as a Component.
For
Instance, A “Bike” usually has common element such as bike color, engine,
mileage etc. in OOP terminology these would be called as a Class Properties or
Attributes of a Bike Object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Bike { //This is the class that contains all properties and behavior of an object //here is some properties of class Bike public string color; public string engine; public int mileage; //here is some behavior of class Bike public string GetColor() { return "red"; } public int GetMileage() { return 65; } } |
Create
simple object and access Bike class.
1 2 3 4 5 | //It also considered as an "Instance of a Bike Class" Bike objBike = new Bike(); //Accessing Bike class methods objBike.GetColor(); objBike.GetMileage(); |
Encapsulation:
Ø
Encapsulation provides the ability to hide the
internal details of an object from its user. The concept of encapsulation is
also known as data hiding or information hiding
Ø
In C#, Encapsulation is implemented using access
modifier keywords
(1)
Public
(2)
Private
(3)
Protected
(4)
Internal
(5)
Protected Internal
How we can achieve Encapsulation?
ð
We achieve Encapsulation by using private access
modifier as shown in below example method
1 2 3 4 5 | private string GetEngineMakeFormula() { private string formula = "a*b"; return formula; } |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public class Bike { public int mileage = 65; public string color = "Black"; private string formula = "a*b"; //Its public – so accessible outside class public int GetMileage() { return mileage; } //Its public – so accessible outside class public string GetColor() { return color; } //Its private – so not accessible outside class private string GetEngineMakeFormula() { return formula; } } public class Program { public static void Main(string[] args) { Bike objBike = new Bike(); Console.WriteLine("Bike mileage is : " + objBike.GetMileage()); //accessible outside "Bike" Console.WriteLine("Bike color is : " + objBike.GetColor()); //accessible outside "Bike" //we can't call this method as it is inaccessible outside "Bike" //objBike.GetEngineMakeFormula(); //commented because we can't access it Console.Read(); } } |
So as
you can see from above code that we hide GetEngineMakeFormula() method by
using private access modifier because is no need to give the make formula to
users. So exposed only necessary methods for user to use it as public access modifier.
Abstraction:
Ø
Abstraction is process of providing only
essential information to the outside real world and hiding overall background
details to present on objects
Examples:
(1)
Bike
ð
We have no access to piston directly, we can
press start button to run the piston. Just imagine if the bike manufacturer
allow direct access to the piston.it would be very difficult to control action
on the piston.
(2)
Gmail
ð
When you provide the username and password and
click the submit button. It will show compose, inbox, outbox, send mails
ete…..When you click compose it will open, but user doesn’t know what are the
action performed internally. That is non-essential thing to user
(3)
TV Remote
ð
Remote is interface between User and TV. It has
the button 0 to 10, on/off etc. but we don’t know circuits inside remove, users
doesn’t need to know. Just he/she using essential things in remote.
Sample Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | public class Bike { public int mileage = 65; public string color = "Black"; private string formula = "a*b"; //Its public – so accessible outside class public int GetMileage() { return mileage; } //Its public – so accessible outside class public string GetColor() { return color; } //Its private – so not accessible outside class private string GetEngineMakeFormula() { return formula; } //Its public – so accessible outside class public string DisplayMakeFormula() { //"GetEngineMakeFormula()" is private but accessible and limited to this class only return GetEngineMakeFormula(); } } public class Program { public static void Main(string[] args) { Bike objBike = new Bike(); Console.WriteLine("Bike mileage is : " + objBike.GetMileage()); //accessible outside "Bike" Console.WriteLine("Bike color is : " + objBike.GetColor()); //accessible outside "Bike" //we can't call this method as it is inaccessible outside "Bike" //objBike.GetEngineMakeFormula(); //commented because we can't access it Console.WriteLine("Bike color is : " + objBike.DisplayMakeFormula()); //accessible outside Console.Read(); } } |
so
you can see from above the code only necessary method and properties exposed
using public access modifier and unnecessary methods and properties are hidden
using private access modifier. This way we can implement abstraction
Inheritance:
Ø
Derived new class from existing class is called
inheritance.
Ø
Derived (sub) class getting all features from
existing (base/ super)class and also
incorporating some new feature to the sub class
(1)
Bike
ð
Bike manufacturer uses same mechanism of
existing version of bike while lunching new version with some additional added
functionality. This allow manufacture to save their time and efforts both
(2)
Company
ð
Consider Head Office act as base class. it have
some rules and regulation and company want to elaborate their business to open
new branch called sub class. It is basically derived from head office. The new
branch have existing rules & regulation with new additional functionality
of their particular branch
Sample Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class Base { public Base() { Console.WriteLine("Constructor of Base Class"); } public void DisplayMessage() { Console.WriteLine("Hello, how are you?"); } } public class Child : Base { public Child() { Console.WriteLine("Constructor of Child class"); } } public class Program { public static void Main(string[] args) { Child objChild = new Child(); //Child class don't have DisplayMessage() method but we inherited from "Base" class objChild.DisplayMessage(); Console.Read(); } } |
As you can see in the previous
example code, We created an object of a Child class in Main() method and then
called DisplayMessage() method of Base class. If you notice that the Child
class doesn’t have DisplayMessage() method in it. So obviously it is inherited
from the Base class. When you execute following code, result would be as show
below:
Sample code Result
Constructor of Base Class
Constructor of Child class
Hello, how are you?
As per sample result, we can say
that the “Base” class constructor will automatically be called before the “Child”
class constructor.
Thus, here conclusion is that
“Base/Parent” classes are automatically instantiated before “Child/Derived”
classes.
Polymorphism:
Ø
Polymorphism means ability to take more than one
form. An operation (method) may exhibit different behaviour in different
situations
Examples:
(1)
Bike
ð
A bike can be into two forms (methods) like cell
start or kick start. We can later on decide which form or method we will use
start bike to go for drive (meaning at runtime)
(2)
Different Behaviour
ð
A single person is behaving differently in in
front of elders and his/her friends
(3)
Stadium
ð
Single stadium but it perform multiple task like
swimming, tennis, cricket, hockey, football etc….
(4)
Software Engineer
ð
A
software engineer can perform different task and different instance of time
depending on the task assigned to him. He can done coding, testing, analysing
and designing depending on the task assign and the requirement.
Type of Polymorphism:
1)
Compile Time Polymorphism
2)
Run Time Polymorphism
1) Compile Time Polymorphism
¨
In this type of polymorphism, compiler identify
which polymorphism form it has to take and execute at compile time is called
compile time polymorphism or early binding
¨
Example
of early binding are method overloading and operator overloading. The Method
overloading means more than one method having same name but different signature
or parameter in same or different class
Advantage:
ó
Execution will be fast because everything about the method known to compiler
during compilation
Disadvantage:
ó It
has lack of flexibility
Sample Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Base { //1st: same method name, return type (object) but different parameter type (object) public object Display(object a) { return (a); } //2nd: same method name, return type (int) but different parameter type (int) public int Display(int a) { return (a); } } public class Program { public static void Main(string[] args) { Base objBase = new Base(); int val = 7; //here 2nd method will be called with type "int" Console.WriteLine(objBase.Display(val)); Console.Read(); } } |
In the above example, when you run the program, Display(int a) method
will be called first because val is of type int at compile time. The assigned
val is only refer to as a int at execution time.
Example Result:
7
2) Runtime Polymorphism
¨
In this type of polymorphism, compiler
identifies which polymorphism for it has to take and execute at run time but
not compile time is called a run time polymorphism or late binding
¨
Example of late binding is method overriding.
The Method overriding means having two methods with same name and same signature.
One method in base class and other method in derived class but both methods
have different functionality
Advantage:
ó
It has flexibility to adjust object type at runtime
Disadvantage:
ó
Execution will be slow as compiler has to get the information about the method
at runtime.
ó
We need to use either virtual or abstract method to allow the derived class to
override a method of base class
Sample Code: [Method by using Virtual]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Base { public virtual string BlogName() { return "AspnetO"; } } public class Child : Base { //same method name with same signature/parameters public override string BlogName() { return "AspnetO – Quick Way To Learn Asp.net"; } } public class Program { public static void Main(string[] args) { Base objBase = new Child(); Console.WriteLine(objBase.BlogName()); Console.Read(); } } |
Sample Code: [Method by using Abstract]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public abstract class Base { public abstract string BlogName(); } public class Child : Base { //same method name with same signature/parameters public override string BlogName() { //It's mandatory to implement abstract method in derived/child class return "AspnetO – Quick Way To Learn Asp.net"; } } public class Program { public static void Main(string[] args) { Base objBase = new Child(); Console.WriteLine(objBase.BlogName()); Console.Read(); } } |
In above example, when you run
the program, at compile time the type of ObjBase is Base but it will still call
child ‘s override method because at the runtime. The type of ObjBase object
refers to is Child
Sample Code Result:
AspnetO – Quick Way To Learn Asp.net
Constructor:
Ø
Constructors is special kind of methods
Ø
It will call when new instance is created
Ø
Constructor can never return anything
Ø
Constructor can be overloaded
Default Constructor of Bike
Class:
1 2 3 | public bike() { } |
Parameter Constructor of Bike
Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class bike { private int mileage; private string color; public bike() { //constructor without parameter } public bike(int mil, string col) { //constructor with two parameters "mil" and "col" mileage = mil; color = col; } public void DisplayBikeData() { Console.WriteLine("Bike's Mileage is " + mileage + " and color is " + color); } } |
Destructors:
Ø
Since garbage clean-up is automatic system.
Framework will free the object that are no longer in use BUT there may be times
where we need to do some manual clean-up. In this case we can use Destructor,
which is used to destroy the object that we no longer want to use
Ø
A destructor method called once an object is
disposed, can be clean-up resources used by the object.
Sample Code:
1 2 3 4 5 6 7 8 9 10 11 | public class Bike { public Bike() { //Constructor } ~Bike() { //Destructor } } |
Once
the class is instantiated, Constructor will be called and when object is
collected by the garbage collector, Destructor method will be called
Reference Program Download Location:
https://github.com/manikandan103222/CSharp-OOPS-Concept-Programs.git
https://github.com/manikandan103222/CSharp-OOPS-Concept-Programs.git
Reference Program Output:
If you like this post. Kindly share your feedback.
No comments:
Post a Comment