Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

Presented By: Bipin Dhakal

Advance
Object Oriented Program (OOP)
 OOP is a programming concept that works on the
principles of abstraction, encapsulation, inheritance, and
polymorphism.
 The basic concept of OOPs is to create objects, re-use
them throughout the program, and manipulate these objects
to get results.
It allows the decomposition of a problem into a number of
entities called objects and then builds data and functions
around these objects. Data cannot be entered directly and
are only accessible through member function.
Features and Characteristics of OOP

 Focuses on data rather than procedures.


 Programs are divided into objects.
 Data is hidden and cannot be excessed by external
functions.
 Objects are communicating with each other through
functions.
Concept of Object Oriented Program
• Objects
• Class
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
Objects
• They are run time entity in an object oriented system.
• They may represent a animal, a person, a table of data or any
item that the program has to handle.
• Program objects should be chosen such that they match
closely with the real world objects.
• The data of the object can be accessed only by the functions
associated with that object.
Class
• In OOP languages, it is mandatory to create a class for
representing data.
• Class is a blueprint of/ for creating an object that contains
variables for storing data and functions to perform
operations on the data.
• A class is a collection of objects of similar type.
• A class has any number of objects belonging with that class.
• Objects are variable of the type class.
• Eg; Fruit apple where apple is an object belonging to the
class fruit.
Abstraction
• It is a mechanism to provide/ of representing the essential
features without describing/including the background details;
means provide the functions to access the hidden (private)
data.
• It is the process of hiding certain details and showing only
essential information to the user. Abstraction can be achieved
with either abstract class or interface.
• It also means putting all the variables and methods in a class
that are necessary.
Encapsulation
• It is the process of binding the data and function together into
a single unit.
• Encapsulation means protect important data inside the class
which we do not want to expose outside the class.
• Means process of binding the data members (variable,
properties) and member function (methods) in a single unit.
• The data is not accessible from the outside, only those
functions which are wrapped in the class can excess.
Abstraction VS Encapsulation
Abstraction Encapsulation
It is used to hide unwanted data It binds data members and
and shows only the required member functions into a single
properties and methods. unit to prevent outsiders from
accessing it directly.
Inheritance
• Inheritance is the process by which object of one class
acquires the property of object of another class.
• The concept of inheritance provides the idea of reusability.
• It means we can add additional feature to an existing class
without modifying it by deriving a new class from existing
one.
• Now the new class will have the combine feature of both
classes.
Polymorphism
• Polymorphism means the ability to take more than one form.
• An operation may exhibit different behavior in different
instant. So, it is also called overloading like operator
overloading or function overload.
• It is the feature that allows one interface to be used for a
general class of action.
• This concept often expressed as “one interface multiple
actions”.
• In the following example, a class has two methods with the same name "Add" but
with different input parameters (the first method has three parameters and the
second method has two parameters).
C# Class
Access Modifier:
• They are applied to the declaration of the class, method,
properties, fields and other members.
• They define the accessibility of the class and its members.
• public, private, protected and internal are access modifiers
in c#.

C# Field:
• The field is a class-level variable that holds a value.
• Generally, field members should have a private access
modifier and used with property.
Constructor:
• A class can have parameterized and parameter less constructor.
• The constructor will be called when you create an instance of a class.
• Constructor can be defined by using an access modifier and class name.
<access_modifier> <class_name>()
{
}
Eg; class myClass{
public myClass()
{
}
}
Method:
• A method can be defined using the following template.
• {access_modifier}{return_type}
methodName(parameterType parameterName)
Eg; public void myMethod(int param1, param2)
{
//method code
}
Property:
• A property can be defined using getters and setters.
• Property encapsulates a private field.
• It provides getters ( get{} ) to retrieve the value of
underlying field and setters ( set{} ) to get the value of the
underlying field.
Auto Implemented Property:
• For property declaration it has been made easy if we don’t
want to apply some logic in get or set.
• There is no private backing field in it. The backing field will
be created automatically by the compiler.
• We can work with an automated property as we would with a
normal property of the class.
Creating Object in C#
• An object is created from a class.
• To create an object of class ,let’s say ‘Car’, specify the class name followed
by the object name and use the keyword ‘new’.
Using multiple class:

• In the above example, we have two classes: Car and Program. Here, we are creating an


object objCar of the Car class in the Program class.
• We have used the  objCar  object to access the members of the Car class from Program. This
is possible because the members in the Car class are public.
• Here, public is an access specifier that means the class members are accessible from any other
classes.
Constructor Types
Default Constructor:
• If we create a constructor without having any parameters,
then we call it a default constructor.
• In case we won’t define any constructor in a class, compiler
will create a default constructor.

Parameterized Constructor:
• If we create a constructor with at least one parameter, then
we call it a parameterized constructor.
Constructor Overloading:
• We can overload the constructor by crating another
constructor with the same method name but with different
parameters.
• When more than one constructor with the same name is
defined in the same class, they are called overloaded, if the
parameters are different for each constructor.
Destructor
• Destructor in C# is a special method inside the class used to destroy
the object or instance of that class when they are no longer needed.
• It will invoke automatically whenever the class instances becomes
unreachable.
• It can be defined only once in a class. Like constructor it is invoked
automatically.
• In C# we can never call the destructor, the reason is one cannot
destroy an object. It’s the .NET frameworks Garbage Collector(GC)
who has the control over the destructor in C#.
• Destructor is created with the same class name using the tilde(~)
operator.
The this Reference
The ‘this’ keyword in C# is used to refer the current instance
of the class.
It is also used to differentiate between the method
parameters and fields if they both have the same name.
Another usage of ‘this’ keyword is to call another
constructor from a constructor in the same class.
Abstract Class
 If a class is defined as abstract then we can’t create an instance of
that class.
 By the creation of derived class object where an abstract class is
inherit from, we can call the method of the abstract class.
Syntax:
abstract class class_name
{
//data member and properties
//methods
}
Interface in C#
Interface in C# is a blueprint of a class.
An interface looks like a class, but has no implementation.
It is like abstract class because all the methods which are declared inside the interface
are abstract methods.
It cannot have method body and cannot be instantiated.
It is used to achieve multiple inheritance which can't be achieved by class.
Syntax:
Interface interface_name
{
//method signature
}
Class class_name : interface_name
{
//method implementation
}
Dependency
• Also called a using relationship, which means, one class is dependent
on another class.
• Dependency is defined as a relation between two classes, where one
class depends on another class but another class may or not may
depend on the first class. So any change in one of the classes may
affect the functionality of the other class, that depends on the first one.
• In a UML representation it is represented by the following symbol;
Generalization
• It is also referred to as a "is-a-kind-of" or "is-a" relationship; that
means that the child class is-a-kind-of a base class.
• This provides the ability to define common properties for derived
classes in a common place and use of them depends on the derived
class requirements.
• In a UML representation it is represented by the following symbol;
Generics
Generic Class:
• The generic class can be defined by putting the <T> sign after the
class name.
• It is not mandatory to put the “T” word in the Generic type
definition. We can use any keyword in the TestClass <> class
declaration.
public class TestClass<T> {}
• The System.Collection.Generic namespace also defines a number of
classes that implement many of these key interfaces.
Define Generic Class:
class StoreProducts<T>
{
public T Product { get; set; }
}

Generic Class with Multiple Type Parameters


class KeyValuePair<TKey, TValue>
{
public TKey Key { get; set; }
public TValue Value { get; set; }
}
Instantiating Generic Class
• We can create an instance of generic classes by specifying an actual type in angle
brackets. The following creates an instance of the generic class StoreProducts;
StoreProducts <string> store = new StoreProducts <string>();

StoreProducts<string> store = new StoreProducts<string>();


class StoreProducts<T>
{
public T Product { get; set; }
}

Eg1; store.Product = “Ball”;

Eg2; KeyValuePair<int, string> kvp = new KeyValuePair<int, string>();


kvp.Key = 100;
kvp.Value = "Hundred";
Generic Method:
• A generic method is a method that is declared with type parameters, as
follows:
static void Swap<T>(ref T a, ref T b)
{
T temp;
temp = a;
a = b;
b = temp;
}
• The objective of this example is to build a swap method that can operate on
any possible data type (value-based or reference-based) using a single type
parameter.
• Due to the nature of swapping algorithm, the incoming parameters will be
sent by reference via ‘ref’ keyword.
Output:
Before Swap:10,20
After Swap:20,10
The following table describes the core class types of this namespace;
Generic class Description

Collection<T> The basis for a generic collection compares two


generic objects for equality

Dictionary<Tkey, Tvalue> A generic collection of name/value pairs

List<T> A dynamic resizable list of items

Queue<T> A generic implementation of a First-In, First-


Out (FIFO) list

Stack<T> A generic implementation of a Last-In, First-


Out (LIFO) list
Association
• Also called a "has-a" relationship that says one class is somehow
associated with another class.
• Association is a special kind of relationship and is sub-divided into the
two specialized concepts of;
1. Aggregation
2. Composition
• In a UML representation it is represented by the following symbol;
Aggregation
• Aggregation is the same as association but with an additional point
that there is an ownership of the instances, unlike association where
there was no ownership of the instances.
• Aggregation is also referred to as a Weak Association.
• In a UML representation it is represented by the following symbol;
Composition
• This is the same as that of aggregation, but with the additional point
that the lifetime of the child instance is dependent on the owner or the
parent class instance.
• Composition is also referred to as a Strong Association or Death
relationship.
• In a UML representation it is represented by the following symbol;

You might also like