Unit - V
Unit - V
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
• The self parameter is a reference to the current instance of the class, and is used to access variables that belong to
the class.
Object methods
• 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__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
Data encapsulation
• Using object-oriented programming in Python we can restrict access to
methods and variables. This prevents data from direct modification. This is
called as encapsulation.
• The objects attributes if private are not directly visible outside the class
definition. This is called data hiding or data abstraction
• Private attributes of a class are denoted with the underscore prefix.
e.g. __max
Class Computer:
def __init__(self):
self.__price=800
Inheritance
• Inheritance allows us to define a class that inherits all the methods and properties
from another class.
• Parent class is the class being inherited from, also called base class.
• Child class is the class that inherits from another class, also called derived class.
• Any class can be a parent class
• To create a class that inherits the functionality from another class, send the parent
class as a parameter when creating the child class:
class Student(Person):
pass
• If __init__ function is added to the child class it will no longer inherit the __init__
function of the parent class
Types of Inheritance
Polymorphism
print(add(2,3))
print(add(2,3,4))
Method overriding
• Composition enables you to reuse code by adding objects to other objects, as opposed to
inheriting the interface and implementation of other classes.
Composition v/s Inheritance
• Composition allows composite classes to reuse the implementation of the components it contains. The
composite class doesn’t inherit the component class interface, but it can leverage its implementation.
• The composition relation between two classes is considered loosely coupled. That means that changes to
the component class rarely affect the composite class, and changes to the composite class never affect
the component class.
• This provides better adaptability to change and allows applications to introduce new requirements
without affecting existing code.
• When looking at two competing software designs, one based on inheritance and another based on
composition, the composition solution usually is the most flexible.
• Inheritance is used where a class wants to derive the nature of parent class and then modify or extend
the functionality of it. Inheritance will extend the functionality with extra features allows overriding of
methods, but in the case of Composition, we can only use that class we can not modify or extend the
functionality of it. It will not provide extra features. Thus, when one needs to use the class as it without
any modification, the composition is recommended and when one needs to change the behavior of the
method in another class, then inheritance is recommended.