Object-Oriented Programming 1
Object-Oriented Programming 1
Object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the
concept of "objects", which can contain data, in the form of fields (often known
as attributes or properties), and code, in the form of procedures (often known as
methods). It is the programming concept based on bottom-up approach!
The main aim of OOP is to bind together the data and the functions that operate
on them so that no other part of the code can access this data except that
function.
Object-oriented programming
Pillars of object-oriented programming
1)Encapsulation:
2)Inheritance:
3)Polymorphism:
4)Abstraction:
5)*Exception Handling
Function/Method
Before going into depth in OOP, let’s understand the 6 basic things!
1. What is class
2. What is object
3. What is namespaces
4. What is function/Method
5. What is Constructor and its types
6. What is Destructors
Function/Method
Class:
It is a user-defined data type, which holds its own data members and member functions, which can be accessed
and used by creating an instance of that class. A class is like a blueprint for an object.
In simple words, it is a prototype or building block structure for starting work in the OOP approach!
Object:
An Object is an identifiable entity with some characteristics and behavior. An Object is an
instance of a Class. When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Employee obj = new Employee();
obj.EmployeeFunction1();
}}
class Employee
{ public void EmployeeFunction1()
{ Console.WriteLine ("Employee
Function1");
}
}
Function/Method
Function VS Method:
A method is a code block that contains a series of statements according to the requirement
of the program! In OOP = Method and in procedure = Function
Namespaces
Namespaces in C# are used to organize too many classes so that it can be easy to handle the
application. We use System.Console where System is the namespace and Console is the
class. To access the class of a namespace, we need to use namespacename.classname. We
can use using keyword so that we don't have to use complete name all the time.
Constructor and its types
A constructor is a special method of the class which gets automatically invoked whenever an instance of
the class is created. Like methods, a constructor also contains the collection of instructions that are
executed at the time of Object creation. It is used to assign initial values to the data members of the
same class.
Types of Constructor
• Default Constructor
• Parametrized Constructor (with overload concept)
• Copy Constructor
• Private Constructor
• Static Constructor
Constructor and its types
Default Constructor
A constructor with no parameters is called a default
constructor. A default constructor has every instance of
the class to be initialized to the same values. The
default constructor initializes all numeric fields to zero
and all string and object fields to null inside a class.
Constructor and its types
Copy Constructor
This constructor will creates an object by copying
variables from another object. Its main use is to
initialize a new instance to the values of an existing
instance.
Constructor and its types
Private Constructor
If a constructor is created with private specifier is known as Private
Constructor. It is not possible for other classes to derive from this
class and also it’s not possible to create an instance of this class.
Points To Remember :
use private constructor when we have only static members.
Using private constructor, prevents the creation of the instances of
that class.
Points To Remember :
It can’t be called directly.
When it is executing then the user has no control.
It does not take access modifiers or any parameters.
It is called automatically to initialize the class before the first instance created.
Constructor
Important points to Remember About Constructors
• Constructor of a class must have the same name as the class name in
which it resides.
• A constructor can not be abstract, final, and Synchronized.
• Within a class, you can create only one static constructor.
• A constructor doesn’t have any return type, not even void.
• A static constructor cannot be a parameterized constructor.
• A class can have any number of constructors.
• Access modifiers can be used in constructor declaration to control its
access i.e. which other class can call the constructor.
• In c#, destructors can be used only in classes and a class can contain only one destructor.
• The destructor in class can be represented by using tilde (~) operator
• The destructor in c# won’t accept any parameters and access modifiers.
• The destructor will invoke automatically, whenever an instance of class is no longer needed.
• The destructor automatically invoked by garbage collector whenever the class objects that
are no longer needed in application.
Till Now, We have Learn!
What is class
What is object
What is namespaces
What is function/Method
What is Constructor and its types
What is Destructor
1)Encapsulation:
2)Inheritance:
3)Polymorphism:
4)Abstraction:
5)*Exception Handling
Encapsulation
It can be access by Access Modifier / Access Specifier, so, in the C# there are Six access Modifier in C# whereas just for
information 4 in Java!
1. Public
2. Private
3. Protected
4. Internal
5. Protected Internal
6. Private Protected
Using Private access modifier, we can Encapsulation the Code (Class, Method etc)…