Object Oriented Programming.pptx
Object Oriented Programming.pptx
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
• Object can move freely within member • Data can move freely from function to
functions function within programs
• 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
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:
class Car:
# class attribute
wheels = 4
#Create an object from the 'Car' class by passing style and color class
Car:
# class attribute
wheels = 4
• Change color
• Start engine
• Stop engine
• Change gear
class Car:
# class attribute
wheels = 4
# 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
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