Professional Documents
Culture Documents
Advance Programming: Presented By: Bipin Dhakal
Advance Programming: 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
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:
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; }
}