Dsa - Topic7 - Oop
Dsa - Topic7 - Oop
ALGORITHM
LESSON 7
OBJECT ORIENTED
PROGRAMMING (OOP)
LESSON 7
• Object Oriented Programming (OOP) is a
programming model where programs are
organized around objects and data rather
than action and logic.
• OOP allows decomposition of a problem
into a number of entities called objects and
then builds data and functions around
these objects.
• A class is the core of any modern Object
OBJECT Oriented Programming language such as
ORIENTED C#.
PROGRAMMING
(OOP) • In OOP languages it is mandatory to create
a class for representing data.
• A class is a blueprint of an object that
contains variables for storing data and
functions to perform operations on the
data.
• A class will not occupy any memory space
and hence it is only a logical representation
of data.
BASIC PROGRAMMING CONCEPTS
IN OOP
• Abstraction
• Polymorphism
• Encapsulation
• Inheritance
The abstraction is simplifying complex reality by modeling classes appropriate to
the problem. The polymorphism is the process of using an operator or function in
different ways for different data input. The encapsulation hides the
implementation details of a class from other objects. The inheritance is a way to
form new classes using classes that have already been defined.
C# CLASS
class ClassName {
A class can contain
• fields - variables to store data
• methods - functions to perform
}
specific tasks
✓The fields and methods inside a class are
called members of a class.
EXAMPLE
class Dog {
In the example,
//field • Dog - class name
string breed;
• breed - field
//method
public void bark() {
• bark() - method
}
}
C# OBJECTS
class Dog {
string breed;
Console.ReadLine();
}
}
}
namespace ClassObject
{
class Employee
{
string department;
}
static void Main(string[] args)
{
MULTIPLE
// create Employee object
Employee sheeran = new Employee();
OF A sheeran.department = "Development";
Console.WriteLine("Sheeran: " + sheeran.department);
Console.ReadLine();
}
}
OBJECTS
IN A
DIFFERENT
CLASS
C# METHOD
void display() {
// code
}
// calls the method
display();
namespace Method {
class Program {
// method declaration
public void display() {
Console.WriteLine("Hello World");
}
//call method
p1.display();
Console.ReadLine();
}
}
}
C# METHOD RETURN TYPE
class Program {
// method declaration
static int addNumbers() {
int sum = 5 + 14;
return sum;
EXAMPLE: }
METHOD
static void Main(string[] args) {
RETURN
TYPE // call method
int sum = addNumbers();
Console.WriteLine(sum);
Console.ReadLine();
}
}
}
EXAMPLE
C# METHODS PARAMETERS
namespace Method{
class Program
{
int addNumber(int a, int b)
{
int sum = a + b;
return sum;
}
//call method
int sum = p1.addNumber(100, 100);
Console.ReadLine();
}
}}
namespace Method{
class Program
{
string work(string work)
{
return work;
//call method
string work = p1.work("Cleaning"); ;
Console.ReadLine();
}
}}
C# ACCESS
MODIFIERS
Access modifiers
specify the
accessibility of types
(classes, interfaces,
etc) and type
members (fields,
methods, etc).
TYPES OF ACCESS MODIFIERS
• 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.
class Program
{
static void Main(string[] args)
{
new Being();
new Being("Tom");
}
class Being
{
public Being()
EXAMPLE {
Console.WriteLine("Being is created");
}
class Circle
{
public Circle(int radius)
{
Console.WriteLine("Circle, r="+ radius + "
is created");
}
}
C# OBJECT INITIALIZERS
}
}