Basic Concept of OOPs Example with C#
About me:
This is Krishna Singh .I’m software engineer student.my Article is
main mission for know about the program and increase our knowledge and helpful
for u…
C-sharp corner provide the best platform of all developer and
knowledgeable software engineers share
your best knowledge to each other and all over world online services. This is
very very best platform.
Background
this Article:
There are some good articles out there, as I see it, newcomers
will always struggle to understand the precise definition of a new concept.
This article is an effort to provide an accurate information pool for new
developers on the basics of, focusing
on Object Oriented Programming (OOP). This article is for you.
Ø Concepts of oops?
Ø Concepts of objects?
Ø Concepts
of classes?
Ø Concepts of Encapsulation?
Ø Concepts of Abstraction?
Ø Concepts of Inheritance?
Ø Concepts of Polymorphism?
Ø Concepts of Constructor and Destructor?
Concepts of oops?
(OOPs) stands
on Object Oriented Programming Structure.it is provide real time program
concept. In order to clearly understand the object orientation, let’s take a
car as an example. “Student” is a class and student age and student address is
an object…
Fig: This Concept is the four main Gods of OOP in
Software Worlds.
Concepts of
objects?
“An object can be consider to “thing”
that can perform set of these activities and all the details of the consider
“thing”. That the object performs defines the object's behavior.
“An object is an
instance of class”.
“It is real
time entities”.
For example:
Public class
Student
{
}
Student objstudent=new
Student ();
Concepts of
classes?
“A class is
blueprint from which the individual an object are created.”
“A class is simply representation
of a type of an object.”
“A class is a
declaration a template or a blueprint that can be used to classify objects.”
“A class is composed of these things, a
name, attributes and operations.”
Example:
Public class
Student
{
int age , address;
string name;
}
Student
objstudent=new Student ();
According to
the sample given below we can say that the Student object
named objstudent, has been created out of the Student class.
Concepts of Encapsulation?
“An
Encapsulation is packaging one or more components together.”
An Encapsulation provides data Representation flexibility by:
ü Hiding the implementation details.
ü Forcing the user to use an Inter face to
access data.
ü Making the code more maintainable.
“Other ways that an encapsulation can
be used, as an example we can take the usage of an interface. The interface can
be used to hide the information of an implemented class.”
The
Encapsulation create class, the class expose public method and properties.
“An Encapsulation is process of binding the
data member and member function into a single unit.”
Example:
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
Encapsulation
{
class Rectangle
{
//public
member variable
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length:{0}",length);
Console.WriteLine("Width:{0}",width);
Console.WriteLine("Area:{0}",GetArea());
}
//end of the class Rectangle
class ExcuteRectangle
{
public static void Main(string[]
args)
{
Rectangle r = new Rectangle();
r.length=4.5;
r.width=5.6;
r.Display();
Console.ReadLine();
}
}
}
}
See Below
Output…
Types of Access Specified:
.Net has five
access Specifies
Public --
Accessible outside the class through object reference.
Private --
Accessible inside the class only through member functions.
Protected --
Just like private but Accessible in derived classes also through member
functions.
Internal --
Visible inside the assembly. Accessible through objects.
Protected
Internal -- Visible inside the assembly through objects and in derived classes
outside the assembly through member functions.
Concepts Of Abstraction?
·
“An
Abstraction is process that involves identifying the critical behavior of an
object and eliminating irrelevant and complex details.”
·
“An
Abstraction is process hiding the implementation details or complex details and
displaying the essential features.”
·
Abstraction
allows you to look at high level details concentrate on relevant
characteristics.
Example:
A Computer consist of many things Such
as Processer, Motherboard, RAM, Keyboard, mouse, Monitor, Wirless,Web Camera,
USB Port, Battery, Speakers, etc. To use it, you don’t need to know now its
works .You just need to know how to operate the Computer, However Computer
another internal parts as well as not easy to use everywhere by everyone.
“So
here the computer is an object that is designed to hide its complexity. “
Concepts of Inheritance?
“On
simple way u can to say “An Inheritance is process of driving the new class
form already existing class.”…..
“An Inheritance allows creating classes which
are derived from other
Classes, so
that they automatically include some of its "parent's" members, plus
its own”.
“The Classes in a Program can be
related with each other & can be refused in various Situations; the process
of creating a new class by adding sum features to an existing class .This
process is called Inheritance.”
For
example, we are going to suppose that we want to declare a series of classes
that describe polygons like our Rectangle, or like
Triangle.
They have certain common properties, such as both can be described by means of
only two sides:
Height
and base.
This
could be represented in the world of classes with a class Polygon from which we
would derive the two other ones: Rectangle and Triangle.
Concepts of Polymorphism?
“Polymorphism it’s
form by and a Greek word & it’s mean many many form.”
“It has ability to
allow a function to exist in different forms.”
“Polymorphism
is the attribute that allows one interface to control access to a general class
of actions.”
“A
Polymorphism the specific action selected is determined by the exact nature of
the situation.”
Example:
Different modes of a person depending upon
the situation, he is in depicting of Polymorphism.
Ø There are two Types of Polymorphism:-
a)
Static
Polymorphism
b)
Dynamic
Polymorphism
Static Polymorphism: - The response to the function is decided
at compile time.
Dynamic Polymorphism: - The response to the function is decided
at run time.
Ø Function Overloading: - it’s means same
name or two more function with different signature (parameter).it’s used to
implementing static polymorphism.
Ø Operator Overloading:-it’s means allow
various operator to be applied to different user defined data types. It’s used
for implementing dynamic polymorphism.
Rules:
Ø The numbers of parameter are different.
void display(int);
void display(int, char);
Ø The data types of parameters are
different.
void display(int);
void display(char);
Ø The Sequence of the parameters are
different.
void
display(int, char);
void display(char,int);
Concepts of Constructor?
“A Constructor
is a special member function whose tasks is to initialize objects of its
class”.it is special because its name is the same as the class name.
“The Constructor
is invoked whenever an object of its associated class is created, it is called
constructor because it construct the value of data members of the class.”
Example:
Class
integer //class with a constructor
{
Int m,n;
Public
integer(void); //constructor is
declared
…..
…..
}
Integer::integer(void) //constructor define
{
M=0;
N=0;
}
Some Special Characteristics of Constructor:
a) They should be declared in the public
section.
b) They are invoked automatically when the
object are created.
c) A default constructor does not have any
parameter but if you need a constructor can have parameters.
d) They do not have return types. They
can’t returns values.
e) They can’t inherited, through the
derived class can call the base class constructor.
f)
Constructor
can’t be virtual.
g) They make implicit calls to the
operators new and delete when memory allocation required.
Note: When a constructor is declared for a
class, initialization of the calls objects becomes mandatory.
Concepts of Destructor?
“Destructor is
also special member function in programming language.”
Destructors
have the opposite function of a constructor. The main use of destructor is to
release dynamic allocated memory. Destructors are used to free memory, release
resources and to perform other clean up. Destructors are automatically named
when an object is destroyed. Like constructor, destructor also the same name as
that of the class name.
Syntax of
Destructor
~classname();
SO Friends
these are few Article that will help you in "Basic concept of OOPs"
next time i will put some more interesting commands thank you..I Hope this
Helpfull for u... enjoy :)