Chapter 5
Chapter 5
Programming in python
Classes And Objects in Python
Class:
Python is an object oriented programming
language.
Almost everything in Python is an object, with
its properties and methods.
A Class is like an object constructor, or a
"blueprint" for creating objects.
To create a class, use the keyword class:
class MyClass:
x=5
Object:
We can use the class named MyClass to create
objects:
p1 = MyClass()
print(p1.x)
The __init__() Function:
Classes and objects in their simplest form are
not really useful in real life applications.
All classes have a function called __init__(),
which is always executed when the class is
being initiated.
Use the __init__() function to assign values to
object properties, or other operations that are
necessary to do when the object is being
created:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
The self Parameter
The self parameter is a reference to the current instance of the
class, and is used to access variables that belongs to the class.
It does not have to be named self , you can call it whatever you
like, but it has to be the first parameter of any function in the
class:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
You can modify properties on objects like this:
p1.age = 40
You can delete properties on objects by using
the del keyword:
del p1.age
You can delete objects by using the del
keyword:
del p1
Method Overloading
Python does not support method overloading
by default. But there are different ways to
achieve method overloading in Python.
The problem with method overloading in
Python is that we may overload the methods
but can only use the latest defined method.
We may define many methods of the same
name and different arguments, but we can
only use the latest defined method. Calling
the other method will produce an error.
def product(a, b):
p=a*b
print(p)
if datatype =='int':
answer = 0
if datatype =='str':
answer =''
answer = answer + x
print(answer)
add('int', 5, 6)
@dispatch(float,float,float)
def product(first,second,third):
result = first * second * third
print(result);
Disadvantages:
When creating more confusion during the
inheritance concepts.
In python, we create a single method with
different arguments to process the Method
Overloading. Here we create a method with zero
or more parameters and also define the methods
based on the number of parameters.
class Area:
def find_area(self,a=None,b=None):
if a!=None and b!=None:
print("Area of Rectangle:",(a*b))
elif a!=None:
print("Area of square:",(a*a))
else:
print("Nothing to find")
obj1=Area()
obj1.find_area()
obj1.find_area(10)
obj1.find_area(10,20)
Method Overriding
Method overriding is a concept of object oriented
programming that allows us to change the implementation of a
function in the child class that is defined in the parent class. It is
the ability of a child class to change the implementation of any
method which is already provided by one of its parent
class(ancestors).
Following conditions must be met for overriding a function:
Inheritance should be there. Function overriding cannot be done
# function breath
def breathe(self):
print("I breathe oxygen.")
# function feed
def feed(self):
print("I eat food.")
# child class
class Herbivorous(Animal):
# function feed
def feed(self):
print("I eat only plants. I am vegetarian.")
herbi = Herbivorous()
herbi.feed()
herbi.breathe()
Data Hiding
Data hiding is a part of object-oriented programming,
which is generally used to hide the data information
from the user. It includes internal object details such as
data members, internal working.
It maintained the data integrity and restricted access
to the class member. The main working of data hiding
is that it combines the data and functions into a single
unit to conceal data within a class.
We cannot directly access the data from outside the
class.
This process is also known as the data
encapsulation. It is done by hiding the working
information to user.
In the process, we declare class members as private so
that no other class can access these data members. It is
accessible only within the class.
In the Python programming, Data hiding
isolates the client from a part of program
implementation. Some of the essential
members must be hidden from the user.
Programs or modules only reflected how we
could use them, but users cannot be familiar
with how the application works. Thus it
provides security and avoiding dependency as
well.
We can perform data hiding in Python using
the __ double underscore before prefix. This
makes the class members private and
inaccessible to the other classes.
class CounterClass:
__privateCount = 0
def count(self):
self.__privateCount += 1
print(self.__privateCount)
counter = CounterClass()
counter.count()
counter.count()
print(counter.__privateCount)
class Tesla(Car):
def mileage(self):
print("The mileage is 30kmph")
class Suzuki(Car):
def mileage(self):
print("The mileage is 25kmph ")
class Duster(Car):
def mileage(self):
print("The mileage is 24kmph ")
class Renault(Car):
def mileage(self):
print("The mileage is 27kmph ")
# Driver code
t= Tesla ()
t.mileage()
r = Renault()
r.mileage()
s = Suzuki()
s.mileage()
d = Duster()
d.mileage()
•An Abstract class can contain the both method normal and
abstract method.
•An Abstract cannot be instantiated; we cannot create objects
for the abstract class.
Inheritance and Composition
It is a concept of Object-
Oriented Programming. Inheritance is a
mechanism that allows us to inherit all the
properties from another class.
The class from which the properties and
functionalities are utilized is called
the parent class (also called as Base Class).
The class which uses the properties from
another class is called as Child Class (also
known as Derived class).
Inheritance is also called an Is-A Relation.
In the given figure, classes are
represented as boxes. The inheritance
relationship is represented by an arrow
pointing from Derived Class(Child
Class) to Base Class(Parent Class).
The extends keyword denotes that
the Child Class is inherited or derived
from Parent Class.
# Parent class
class Parent :
# Constructor
# Variables of Parent class
# Methods
...
...
...
...
# parent class
class Parent:
# parent class method
def m1(self):
print('Parent Class Method called...')