0% found this document useful (0 votes)
18 views42 pages

Dsa - Topic7 - Oop

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views42 pages

Dsa - Topic7 - Oop

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

DATA STRUCTURE AND

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 is the blueprint for the object.


• We can think of the class as a sketch (prototype) of a
house. It contains all the details about the floors,
doors, windows, etc. We can build a house based on
these descriptions. House is the object.
• Like many houses can be made from the sketch, we
can create many objects from a class.
A class declaration contains only a keyword class, followed by
an identifier(name) of the class. In general, class
declarations can include these components, in order:
• Modifiers: A class can be public or internal etc. By default
modifier of the class is internal.
• Keyword class: A class keyword is used to declare the type
class.
• Class Identifier: The variable of type class is provided. The
DECLARATION identifier(or name of the class) should begin with an initial
OF CLASS letter which should be capitalized by convention.
• Base class or Super class: The name of the class’s parent
(superclass), if any, preceded by the : (colon). This is
optional.
• Interfaces: A comma-separated list of interfaces
implemented by the class, if any, preceded by the : (colon).
A class can implement more than one interface. This is
optional.
• Body: The class body is surrounded by { } (curly braces).
CREATE A CLASS IN C#

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

• It is a basic unit of Object-Oriented


Programming and represents real-life
entities.
• An object is an instance of a class. Suppose,
we have a class Dog. Bulldog, German
Shepherd, Pug are objects of the class.
An object consists of :
• State: It is represented by attributes of an object. It
also reflects the properties of an object.
• Behavior: It is represented by the methods of an
object. It also reflects the response of an object with
other objects.
• Identity: It gives a unique name to an object and
C# OBJECTS enables one object to interact with other objects.
When an object of a class is created, the class is said
to be instantiated. All the instances share the
attributes and the behavior of the class. But the
values of those attributes, i.e. the state are unique for
each object. A single class may have any number of
instances.
DECLARING
OBJECTS
CREATE AN OBJECT OF A CLASS

ClassName obj = new ClassName();


namespace ClassObject {

class Dog {
string breed;

public void bark() {


Console.WriteLine("Bark Bark !!");

ACCESS static void Main(string[] args) {


CLASS // create Dog object
MEMBERS Dog bullDog = new Dog();

USING // access breed of the Dog


OBJECT bullDog.breed = "Bull Dog";
Console.WriteLine(bullDog.breed);

// access method of the Dog


bullDog.bark();

Console.ReadLine();

}
}
}
namespace ClassObject
{

class Employee
{

string department;
}
static void Main(string[] args)
{

MULTIPLE
// create Employee object
Employee sheeran = new Employee();

OBJECTS // set department for sheeran

OF A sheeran.department = "Development";
Console.WriteLine("Sheeran: " + sheeran.department);

CLASS // create second object of Employee


Employee taylor = new Employee();

// set department for taylor


taylor.department = "Content Writing";
Console.WriteLine("Taylor: " + taylor.department);

Console.ReadLine();

}
}
OBJECTS
IN A
DIFFERENT
CLASS
C# METHOD

• A method is a block of code that performs a specific task.


Suppose you need to create a program to create a circle
and color it. You can create two methods to solve this
problem:
a method to draw the circle
a method to color the circle
• Dividing a complex problem into smaller chunks makes
your program easy to understand and reusable.
S Y N TA X
In C# a method declaration consists of the following
components as follows :
• Modifier : It defines access type of the method i.e.
from where it can be accessed in your application. In
C# there are Public, Protected, Private access
modifiers.
• Name of the Method : It describes the name of the
user defined method by which the user calls it or
refer it. Eg. GetName()
METHOD • Return type: It defines the data type returned by the
method. It depends upon user as it may also return
DECLARATION void value i.e return nothing
• Body of the Method : It refers to the line of code of
tasks to be performed by the method during its
execution. It is enclosed between braces.
• Parameter list : Comma separated list of the input
parameters are defined, preceded with their data
type, within the enclosed parenthesis. If there are no
parameters, then empty parentheses () have to use
out.
CALLING A METHOD IN C#

void display() {

// code
}
// calls the method
display();
namespace Method {

class Program {

// method declaration
public void display() {
Console.WriteLine("Hello World");
}

static void Main(string[] args) {

EXAMPLE // create class object


Program p1 = new Program();

//call method
p1.display();

Console.ReadLine();

}
}
}
C# METHOD RETURN TYPE

• A C# method may or may not return a


value. If the method doesn't return
any value, we use the void keyword. int addNumbers() {
• If the method returns any value, we ...
use the return statement to return any
value. return sum;
• Here, we are returning the
variable sum. One thing you should
always remember is that the return }
type of the method and the returned
value should be of the same type.
namespace Method {

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;
}

EXAMPLE static void Main(string[] args)


{
// create class object
Program p1 = new Program();

//call method
int sum = p1.addNumber(100, 100);

Console.WriteLine("Sum: " + sum);

Console.ReadLine();
}
}}
namespace Method{
class Program
{
string work(string work)
{
return work;

static void Main(string[] args)


EXAMPLE: {
SINGLE
PARAMETER // create class object
Program p1 = new Program();

//call method
string work = p1.work("Cleaning"); ;

Console.WriteLine("Work: " + work);

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

In C#, there are 4 basic types of access


modifiers.
• public
• private
• protected
• internal
PUBLIC
ACCESS
MODIFIER
PRIVATE
ACCESS
MODIFIER
PROTECTED
ACCESS
MODIFIER
PROTECTED
ACCESS
MODIFIER
INTERNAL ACCESS MODIFIER

• When we declare a type or type member as internal, it can


be accessed only within the same assembly.
• An assembly is a collection of types (classes, interfaces,
etc) and resources (data). They are built to work together
and form a logical unit of functionality.
• That's why when we run an assembly all classes and
interfaces inside the assembly run together.
namespace AccessSpecifiers
{
class InternalTest
{
internal string name = "Shantosh Kumar";
internal void Msg(string msg)
{
Console.WriteLine("Hello " + msg);
}
}
E X AM P LE : class Program
I N TE R NAL {
static void Main(string[] args)
{
InternalTest internalTest = new
InternalTest();
// Accessing internal variable
Console.WriteLine("Hello " +
internalTest.name);
// Accessing internal function
internalTest.Msg("Peter Decosta");
}
}
}
C# CONSTRUCTOR

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.
Unlike methods, a constructor:
• has the same name as that of the class
• does not have any return type
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.
class Program
{
static void Main(string[] args)
{
new Being();
new Being("Tom");
}
class Being
{
public Being()
EXAMPLE {
Console.WriteLine("Being is created");
}

public Being(string being)


{
Console.WriteLine("Being " + being + "is
created");
}
}
}
class Program
{
static void Main(string[] args)
{
var name = "Lenka";
var born = new DateTime(1990, 3, 5);

var friend = new MyFriend(name, born);


friend.Info();
}
EXAMPLE: class MyFriend
{
INITIATE private DateTime born;
DATA private string name;

MEMBERS OF public MyFriend(string name, DateTime born)


THE CLASS {
this.name = name;
this.born = born;
}

public void Info()


{
Console.WriteLine("{0} was born on {1}",
this.name, this.born.ToShortDateString());
}
}
}
C# CONSTRUCTOR CHAINING

Constructor chaining is the ability of


a class to call another constructor
from a constructor. To call another
constructor from the same class, we
use the this keyword.
class Program
{
static void Main(string[] args)
{
new Circle(5);
new Circle();
}

class Circle
{
public Circle(int radius)
{
Console.WriteLine("Circle, r="+ radius + "
is created");

public Circle() : this(1) { }


}

}
}
C# OBJECT INITIALIZERS

Object initializers let us assign values to any


accessible fields or properties of an object at
creation time without having to invoke a
constructor. The properties or fields are
assigned inside the {} brackets. Also, we can
specify arguments for a constructor or omit the
arguments.
class Program
{
static void Main(string[] args)
{
var u = new User { Name = "John Doe",
Occupation = "gardener" };
Console.WriteLine(u);
}
class User
{
public User() {}
EXAMPLE
public string Name { set; get; }
public string Occupation { set; get; }

public override string ToString()


{
return Name + " is a" + Occupation;
}
}

}
}

You might also like