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

3-4

Uploaded by

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

3-4

Uploaded by

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

Object Oriented

Programming
Dr. Bharat Singh
PhD, CSE
• Introduction
• Defining a class
• Fields Declaration
• Methods declaration
• Creating Objects
• Accessing Class Members
• Constructors
• Self argument
• Static Members
What is Object Oriented
Programming?
• Object-oriented programming (OOP) is a programming
paradigm based on the concept of "objects".

• The object contains both data and code: Data in the form of
properties (often known as attributes), and code, in the form
of methods (actions object can perform).
What is Object Oriented
Programming ?

• An object-oriented paradigm is to design the program


using classes and objects.
• Python programming language supports different
programming approaches like functional
programming, modular programming.
• One of the popular approaches is object-oriented
programming (OOP) to solve a programming problem
is by creating objects
What is Object Oriented
Programming in Python
• An object has the following two characteristics:
• Attribute
• Behavior

• For example, A Car is an object, as it has the following properties:


• name, price, color as attributes
• breaking, acceleration as behavior
Class and Objects
• A class is a blueprint for the object. To create an object we require a
model or plan or blueprint which is nothing but class.

• A class contains the properties (attribute) and action (behavior) of the


object. Properties represent variables, and the methods represent
actions. Hence class includes both variables and methods.

• Object is an instance of a class. The physical existence of a class is


nothing but an object. In other words, the object is an entity that has
a state and behavior. It may be any real-world object like the mouse,
keyboard, laptop, etc.
Class and Objects
• Every object has the following properties.
• Identity: Every object must be uniquely identified.
• State: An object has an attribute that represents a state of an object, and it
also reflects the property of an object.
• Behavior: An object has methods that represent its behavior.

An object is a real-life entity.


It is the collection of various data and functions that operate on those
data.
Class and Objects
Class and Objects
Class and Objects
Class and Objects
Create a Class
In Python, class is defined by using the class keyword.

• class class_name:
• '''This is a docstring. I have created a new class'''
• <statement 1>
• <statement 2> class ClassName:
• . initializer
• .
methods
• <statement N>
Example: Define a class in Python
• In this example, we are creating a Person Class with name, sex, and
profession instance variables.
class Person:
def __init__(self, name, sex, profession):
# data members (instance variables)
self.name = name
self.sex = sex
self.profession = profession

# Behavior (instance methods)


def show(self):
print('Name:', self.name, 'Sex:', self.sex, 'Profession:', self.profession)

# Behavior (instance methods)


def work(self):
print(self.name, 'working as a', self.profession)
Example
# Python3 program to demonstrate instantiating# a class
class Person:
# A simple class attribute
attr1 = "smart boy"
attr2 = "smart girl"

# A sample method
def fun(self):
print("I am a", self.attr1)
print("I am a", self.attr2)

# Driver code, Object instantiation


Rodger = Person()

# Accessing class attributes, and method through objects


print(Rodger.attr1)
Rodger.fun()
Create Object of a Class
• The object is created using the class name.
• When we create an object of the class, it is called instantiation.
• The object is also called the instance of a class.

• <object-name> = <class-name>(<arguments>)

• for example
• jessa = Person('Jessa', 'Female', 'Software Engineer')
# Python program to demonstrate instantiating a class
class Person:
# A simple class attribute
attr1 = "smart boy"
attr2 = "smart girl"

# A sample method
def fun(self):
print("I am a", self.attr1)
print("I am a", self.attr2)

def greet(self):
print("hope you are doing well")
# Driver code
# Object instantiation
Rodger = Person()

# Accessing class attributes and method through objects


print(Rodger.attr1)
print(Rodger.attr2)
Rodger.fun()
Rodger.greet()
Object Creation
class Student:

# initialize instance variable


def __init__(self, name):
self.name = name

# instance Method
def show(self):
print('Hello, my name is', self.name)

# create object using constructor


s1 = Student('Emma')
s1.show()
Example: class Person:
def __init__(self, name, sex, profession):
# data members (instance variables)
self.name = name
self.sex = sex
self.profession = profession

# Behavior (instance methods)


def show(self):
print('Name:', self.name, 'Sex:', self.sex, 'Profession:', self.profession)

# Behavior (instance methods)


def work(self):
print(self.name, 'working as a', self.profession)

# create object of a class


jessa = Person('Jessa', 'Female', 'Software Engineer')

# call methods
jessa.show()
jessa.work()
Constructor
• A constructor is a special method used to create and initialize an object of a class.
• Constructors are used to initializing the object’s state.
• This method is defined in the class.
• The constructor is executed automatically at the time of object creation.
• The primary use of a constructor is to declare and initialize data member/ instance
variables of a class. The constructor contains a collection of statements (i.e., instructions)
that executes at the time of object creation to initialize the attributes of an object.

• For example, when we execute obj = ClassName(), Python gets to know that obj
is an object of ClassName and calls the constructor of that class to create an
object.
• On the other hand, a destructor is used to destroy the object.
Constructor
• In Python, Object creation is divided into two parts in Object Creation
and Object initialization

• Internally, the __new__ is the method that creates the object


• And, using the __init__() method we can implement constructor to
initialize the object.
Syntax of a constructor
def __init__(self):
# body of the constructor

• def: The keyword is used to define function.


• __init__() Method: It is a reserved method. This method gets called
as soon as an object of a class is instantiated.
• self: The first argument self refers to the current object.
Create a Constructor in Python
class Student:
• In this example, we’ll
# constructor
create a Class Student # initialize instance variable
with an instance def __init__(self, name):
variable student print('Inside Constructor')
name. we’ll see how self.name = name
print('All variables initialized')
to use a constructor
to initialize the # instance Method
student name at the def show(self):
time of object print('Hello, my name is', self.name)
creation.
# create object using constructor
s1 = Student('Emma')
s1.show()
Constructor Example
# Sample class with init method
class Person:

# init method or constructor


def __init__(self, name):
self.name = name

# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)

p = Person('Nikhil')
p.say_hi()
Constructor
• Note:

• For every object, the constructor will be executed only once. For
example, if we create four objects, the constructor is called four
times.
• In Python, every class has a constructor, but it’s not required to define
it explicitly. Defining constructors in class is optional.
• Python will provide a default constructor if no constructor is defined.
Types of Constructors
• In Python, we have the following
three types of constructors.

1. Default Constructor
2. Non-parametrized constructor
3. Parameterized constructor
Default Constructor
• Python will provide a default constructor if no constructor
is defined. Python adds a default constructor when we do
not include the constructor in the class or forget to
declare it. It does not perform any task but initializes the class Employee:
objects. It is an empty constructor without a body.
def display(self):
• If you do not implement any constructor in your class or print('Inside Display')
forget to declare it, the Python inserts a default
constructor into your code on your behalf. This emp = Employee()
constructor is known as the default constructor. emp.display()

• It does not perform any task but initializes the objects. It is


an empty constructor without a body.
• If you implement your constructor, then the default
constructor will not be added.
Non-Parametrized Constructor
class Company:
• A constructor without any # no-argument constructor
arguments is called a non- def __init__(self):
parameterized constructor. This self.name = "PYnative"
self.address = "ABC Street"
type of constructor is used to
initialize each object with default # a method for printing data members
values. def show(self):
print('Name:', self.name, 'Address:', self.address)

# creating object of the class


• This constructor doesn’t accept the cmp = Company()
arguments during object creation.
# calling the instance method using the object
Instead, it initializes every object cmp.show()
with the same set of values.
Parameterized Constructor
class Employee:
# parameterized constructor
• A constructor with defined parameters or def __init__(self, name, age, salary):
arguments is called a parameterized self.name = name
constructor. We can pass different values self.age = age
to each object at the time of creation self.salary = salary
using a parameterized constructor.
# display object
def show(self):
• The first parameter to constructor is self print(self.name, self.age, self.salary)
that is a reference to the being
constructed, and the rest of the # creating object of the Employee class
arguments are provided by the emma = Employee('Emma', 23, 7500)
emma.show()
programmer.
• A parameterized constructor can have kelly = Employee('Kelly', 25, 8500)
any number of arguments. kelly.show()
Constructor With Default Values
class Student:
# constructor with default values age and classroom
• Python allows us to define a def __init__(self, name, age=12, classroom=7):
constructor with default values. self.name = name
self.age = age
The default value will be used if self.classroom = classroom
we do not pass arguments to the
# display Student
constructor at the time of object def show(self):
creation. print(self.name, self.age, self.classroom)

# creating object of the Student class


emma = Student('Emma')
emma.show()

kelly = Student('Kelly', 13)


Emma 12 7 kelly.show()
Kelly 13 7
Thank You

You might also like