0% found this document useful (0 votes)
8 views

Object Oriented Programming

Learning documents

Uploaded by

Aseem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Object Oriented Programming

Learning documents

Uploaded by

Aseem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C# | Programming Language:

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or methods that perform


operations on the data, while object-oriented programming is about creating
objects that contain both data and methods.

Advantages:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP helps to keep the C# code DRY "Don't Repeat Yourself", and makes
the code easier to maintain, modify and debug.
 OOP makes it possible to create full reusable applications with less code
and shorter development time

Classes & Objects:


A class is a template for objects, and an object is an instance of a class. When
the individual objects are created, they inherit all the variables and methods
from the class. Class contains State(Data) and Behaviour(Function)

class combines the fields and methods(member function which defines actions)
into a single unit. In C#.

Ex: Car(Class) – Volvo(Object)

Syntax:

Class className{} =

Class Car{

Codes

It is a basic unit of Object-Oriented Programming and represents the real-life


entities. A typical C# program creates many objects, which as you know,
interact by invoking methods.

Creating an object :

An object is created from a class. We have already created the class named Car,
so now we can use this to create objects.To create an object of Car, specify the
class name, followed by the object name, and use the keyword new:

EX// Car obj = new Car();


Constructors in class are used for initializing new objects. Fields are
variables that provide the state of the class and its objects, and methods are
used to implement the behavior of the class and its objects.

Constructors:
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.

Example.

class Geek

{
.......

// Constructor

public Geek() {}

.......

// an object is created of Geek class,

// So above constructor is called

Geek obj = new Geek();

Protocols of constructor:
 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.

Types of Constructor:

1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Private Constructor
5. Static 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.

A constructor having at least one parameter is called as parameterized


constructor. It can initialize each instance of the class to different values.

This constructor 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.

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.
 It is the implementation of a singleton class pattern.
 use private constructor when we have only static members.
 Using private constructor, prevents the creation of the instances of
that class.
In object-oriented programming, a singleton class is a class that
can have only one object (an instance of the class) at a time.
Static Constructor has to be invoked only once in the class and it has been
invoked during the creation of the first reference to a static member in the
class. A static constructor is initialized static fields or data of the class and to
be executed only once.
 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.

You might also like