Object Oriented
Object Oriented
Object Oriented
class’s methods.
⊙ Data member: A class variable or instance variable that holds
data associated with a class and its objects.
⊙ Function overloading: The assignment of more than one behavior
to a particular function.
– The operation performed varies by the types of objects or argu-
ments involved.
⊙ Instance variable: A variable that is defined inside a method and
belongs only to the current instance of a class.
⊙ Inheritance: The transfer of the characteristics of a class to other
classes that are derived from it.
⊙ Instance: An individual object of a certain class.
– An object obj that belongs to a class Circle, for example, is an
instance of the class Circle.
⊙ Instantiation: The creation of an instance of a class.
⊙ Method: A special kind of function that is defined in a class
definition.
⊙ Object: A unique instance of a data structure that is defined by
its class.
– An object comprises both data members (class variables and
instance variables) and methods.
⊙ Operator overloading: The assignment of more than one function
to a particular operator.
Class
⊙ The class statement creates a new class definition. The name of the
class immediately follows the keyword class followed by a colon.
⊙ The first string inside the class is called docstring and has a brief
description about the class. Although not mandatory, this is highly
recommended.
class ClassName:
'docstring: Optional class documentation string'
class_suite
⊙ A class parrot contains all the details about parrot. Here, a parrot
is an object.
Constructors
⊙ Class functions that begin with double underscore __ are called
special functions as they have special meaning.
⊙ Of one particular interest is the __init__() function. This special
function gets called whenever a new object of that class is instanti-
ated.
⊙ You declare other class methods like normal functions with the
exception that the first argument to each method is self.
– Python adds the self argument to the list for you; you do not
need to include it when you call the methods.
⊙ Following table lists some generic functionality that you can override
in your own classes
– __init__(self [,args...]): Constructor.
Sample Call: obj = className(args)
– __del__(self): Destructor, deletes an object.
Sample Call: del obj
– __repr__(self): Evaluable string representation.
Sample Call: repr(obj)
– __str__(self): Printable string representation.
Sample Call: str(obj)
– __cmp__(self, x): Object comparison.
Sample Call: cmp(obj, x)
Object
⊙ An object (instance) is an instantiation of a class.
– When class is defined, only the description for the object is
defined.
– Therefore, no memory or storage is allocated.
⊙ The example for object of parrot class can be:
obj = Parrot()
⊙ Example
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
Output:
Blu is a bird
Woo is also a bird
Blu is 10 years old
Woo is 15 years old
Methods
⊙ Methods are functions defined inside the body of a class.
– They are used to define the behaviors of an object.
⊙ Example
class Parrot:
def __init__(self, name, age): # instance attributes
self.name = name
self.age = age
def sing(self, song): # instance method
return "{} sings {}".format(self.name, song)
def dance(self):
return "{} is now dancing".format(self.name)
Output:
Blu sings 'Happy'
Blu is now dancing
Inheritance
⊙ Inheritance is a way of creating a new class for using details of an
existing class without modifying it.
⊙ The newly formed class is a derived class (or child class). Similarly,
the existing class is a base class (or parent class).
⊙ Example
# parent class
class Bird:
def __init__(self):
print("Bird is ready")
def whoisThis(self):
print("Bird")
def swim(self):
print("Swim faster")
# child class
class Penguin(Bird):
def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")
def whoisThis(self):
print("Penguin")
def run(self):
print("Run faster")
peggy = Penguin()
peggy.whoisThis()
peggy.swim()
peggy.run()
Output:
Bird is ready
Penguin is ready
Penguin
Swim faster
Run faster
⊙ Similar way, you can drive a class from multiple parent classes as
follows
class A: # define your class A
.....
Encapsulation
⊙ In OOP, we can restrict access to methods and variables.
– This prevents data from direct modification which is called en-
capsulation.
– We denote private attributes using underscore as the prefix i.e
single _ or double __.
⊙ Example
class Computer:
def __init__(self):
self.__maxprice = 900
def sell(self):
print("Selling Price: {}".format(self.__maxprice))
def setMaxPrice(self, price):
self.__maxprice = price
c = Computer()
c.sell()
# change the price
c.__maxprice = 1000
c.sell()
# using setter function
c.setMaxPrice(1000)
c.sell()
Output:
Polymorphism
⊙ Polymorphism is an ability to use a common interface for multiple
forms.
– Suppose, we need to color a shape, there are multiple shape
options (rectangle, square, circle).
– However we could use the same method to color any shape.
– This concept is called Polymorphism.
⊙ Example
class Parrot:
def fly(self):
print("Parrot can fly")
def swim(self):
print("Parrot can't swim")
class Penguin:
def fly(self):
print("Penguin can't fly")
def swim(self):
print("Penguin can swim")
# common interface
def flying_test(bird):
bird.fly()
#instantiate objects
blu = Parrot()
peggy = Penguin()
# passing the object
flying_test(blu)
flying_test(peggy)
Output:
Parrot can fly
Penguin can't fly