Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Object Oriented Programming.pptx

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, emphasizing the use of objects to structure code and reduce redundancy. It explains the differences between Object-Oriented and Procedural-Oriented Programming, the definition and structure of classes and objects, and the significance of attributes and methods. Additionally, it includes examples of class definitions and object instantiation to illustrate these concepts.

Uploaded by

ahire6687
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Object Oriented Programming.pptx

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, emphasizing the use of objects to structure code and reduce redundancy. It explains the differences between Object-Oriented and Procedural-Oriented Programming, the definition and structure of classes and objects, and the significance of attributes and methods. Additionally, it includes examples of class definitions and object instantiation to illustrate these concepts.

Uploaded by

ahire6687
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

PYTHON OOPs

CONCEPT
WHAT IS OOPs ?
• OOPs refers to languages that use objects in programming, they use
objects as a primary source to implement what is to happen in the code.
• Objects are seen by the viewer or user, performing tasks assigned by you.
• Object-oriented programming (OOP) is a method of structuring a
program by bundling related properties and behaviors into
individual objects.
• The main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data
except that function.
• OOP focuses on the objects that developers want to manipulate rather
than the logic required to manipulate them. This approach to programming
is well-suited for programs that are large, complex and actively updated or
maintained.
Some Major Benefits of OOPS

• They reduce the redundancy of the code by writing clear and


re-usable codes (using inheritance).

• They are easier to visualize because they completely relate to


real-world scenarios. For example, the concept of
objects, inheritance, abstractions, relate very closely to
real-world scenarios(we will discuss them further in this
article).

• Every object in oops represent a different part of the code and


have their own logic and data to communicate with each other.
So, there are no complications in the code.
Difference between Object-Oriented and
Procedural Oriented Programming
Object-Oriented Programming (OOP) Procedural-Oriented Programming (Pop)

• It is a bottom-up approach • It is a top-down approach

• Program is divided into objects • Program is divided into functions

• Makes use of Access modifiers


• Doesn’t use Access modifiers
‘public’, private’, protected’

• It is more secure • It is less secure

• Object can move freely within member • Data can move freely from function to
functions function within programs

• It supports inheritance • It does not support inheritance


CLASS
• The class can be defined as a collection of objects. It is a logical entity that has
some specific attributes and methods. For example: if you have an employee
class, then it should contain an attribute and method, i.e. an email id, name, age,
salary, etc.

• A class is used to create user-defined data structures in Python. Classes define


functions, which are termed as methods, that describe the behaviors and actions
that an object created from a class can perform. OOPS concepts in python
majorly deals with classes and objects.

• Classes make the code more manageable by avoiding complex codebases. It does
so, by creating a blueprint or a design of how anything should be defined. It
defines what properties or functions, any object which is derived from the class
should have.
CLASS
A class description consists of two things :
• Attributes or member variables
• Implementations of behavior or member functions
• Class Definition Syntax:

class ClassName:
# Statement-1
.
.
.
# Statement-N
DEFINE A CLASS
Classes in Python can be defined by the keyword class, which is followed by the
name of the class and a colon.
Syntax:
class class_name:
class body

Class Human:
pass

• indented code above the class definition is considered part of the class body.
• 'pass' is commonly used as placeholders, in the place of code whose
implementation we may skip for the time being. "pass" allows us to run the code
without throwing an error in Python.
OBJECT
• Object Oriented Programming is a way of computer programming using the idea
of “objects” to represents data and methods. It is also, an approach used for
creating neat and reusable code instead of a redundant one. the program is
divided into self-contained objects or several mini-programs. Every Individual
object represents a different part of the application having its own logic and data
to communicate within themselves.
• Object is a single instance of a class, which contains data and methods working
on the data. So an object consists of three things:
• Name: This is Variable name that represents the object.
• Member Data: The data that describes the object.
• Member Method: Behavior that describes the object.
• For Example: Samsung Galaxy is an object with the brand name Samsung, 2 gb
RAM as properties and calling and texting as behavior.
• An object is a collection of data (variables) and methods (functions) that access
the data. It is the real implementation of a class.

CLASS AND OBJECT
The __init__() Method

• The _init_() is special method that initializes an individual object. This


method runs automatically each time an object of a class is created.

• The _init_()method is generally used to perform operations that are


necessary before the object is created.
class Car:
# initializer
def __init__(self):
pass
• When you define _init_() in a class definition, its first parameter should
be self.
The self Parameter

• The self parameter refers to the individual object itself. It is used to


fetch or set attributes of the particular instance.

• This parameter doesn’t have to be called self, you can call it


whatever you want, but it is standard practice, and you should
probably stick with it.
Example of Class and Object
Suppose you want to create objects to represent information about cars.
Each object will represent a single car. You’ll first need to define a class
called Car.

Here’s the simplest possible class (an empty one):

class Car:
pass
Attributes
• Every class you write in Python has two basic
features: attributes and methods.
• Attributes are the individual things that differentiate one object
from another. They determine the appearance, state, or other
qualities of that object.

• In our case, the ‘Car’ class might have the following attributes:

• Style: Sedan, SUV, Coupe


• Color: Silver, Black, White
• Wheels: Four
# A class with one class attribute

class Car:

# class attribute
wheels = 4

# initializer with instance attributes

def __init__(self, color, style):


self.color = color
self.style = style
You create an object of a class by calling the class name and passing
arguments as if it were a function.

#Create an object from the 'Car' class by passing style and color class
Car:

# class attribute
wheels = 4

# initializer with instance attributes


def __init__(self, color, style):
self.color = color
self.style = style
c = Car('Sedan', 'Black’)
Methods

• Methods determine what type of functionality a class has,


how it handles its data, and its overall behavior. Without
methods, a class would simply be a structure.

• In our case, the ‘Car’ class might have the following


methods:

• Change color
• Start engine
• Stop engine
• Change gear
class Car:

# class attribute
wheels = 4

# initializer / instance attributes


def __init__(self, color, style):
self.color = color
self.style = style

# method 1
def showDescription(self):
print("This car is a", self.color, self.style)

# method 2
def changeColor(self, color):
self.color = color

c = Car('Black', 'Sedan’)

# call method 1
c.showDescription()
# Prints This car is a Black Sedan

# call method 2 and set color


c.changeColor('White’)

c.showDescription()
# Prints This car is a White Sedan
Example Of Class Human
class Human:
species = "Homo Sapiens“
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
# x and y are instances of class Human
x = Human("Ron", 15, "male")
y = Human("Miley", 22, "female")
print(x.species)
# species are class attributes, hence will have same value for all instances
print(y.species)
# name, gender and age will have different values per instance, because they are
instance attributes
print(f"Hi! My name is {x.name}. I am a {x.gender}, and I am {x.age} years
old")
print(f"Hi! My name is {y.name}. I am a {y.gender}, and I am {y.age} years old")
THANK
YOU

You might also like