0% found this document useful (0 votes)
14 views

Unit - V

The document provides an overview of Object Oriented Programming (OOP) in Python, highlighting key principles such as encapsulation, inheritance, and polymorphism. It explains the concepts of classes and objects, the use of the __init__ function, and the differences between composition and inheritance. Additionally, it covers method overloading and overriding, emphasizing the importance of code reusability and adaptability in software design.

Uploaded by

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

Unit - V

The document provides an overview of Object Oriented Programming (OOP) in Python, highlighting key principles such as encapsulation, inheritance, and polymorphism. It explains the concepts of classes and objects, the use of the __init__ function, and the differences between composition and inheritance. Additionally, it covers method overloading and overriding, emphasizing the importance of code reusability and adaptability in software design.

Uploaded by

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

Unit - V

Object Oriented Programming in Python


12 marks
Object Oriented Paradigm
• Python is an object-oriented programming language.
• Object oriented programming allows creating reusable code.
• Basic principles of OOP:
• Encapsulation – Hiding the private details of a class from other objects
• Inheritance – creating new classes from existing classes
• Polymorphism – using common operations in different ways for different data
Classes and Objects
• Almost everything in Python is an object, with its properties and methods.
• Class – A user defined prototype for an object that defines a set of
attributes (data members and methods) that characterize any object of the
class.
• Create class
class box:
side = 5
• Create object
b1 = box()
print(b1.side)
Built-in class attributes
• __dict__ − Dictionary containing the class's namespace.
• __doc__ − Class documentation string or none, if undefined.
• __name__ − Class name.
• __module__ − Module name in which the class is defined. This
attribute is "__main__" in interactive mode.
• __bases__ − A possibly empty tuple containing the base classes, in
the order of their occurrence in the base class list.
The __init__ function
• All classes have a function called __init__(). It is called automatically every time the class is being used to create a
new object.
• The __init__() function is used 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)

• 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

Polymorphism in object-oriented programming is an ability to


use common interface for multiple forms
• Using in-built polymorphic functions (for e.g. len() function)
• Using user defined functions (Method overloading)
• Using functions and objects in inheritance (Method overriding)
Method overloading
• To overload a method in python, the method logic is written in such a way
that depending upon the parameters passed, the code executes
accordingly
• It is used in a class where the method may need to handle different
number of parameters at runtime

def add(x, y, z=0):


return x+y+z

print(add(2,3))
print(add(2,3,4))
Method overriding

• Method overriding is a way of implementing polymorphism in


inheritance.
• Using this, a derived class can create a method with the same name
as in the base class but with different programming logic.
• Thus, the concept of method overriding allows to change or override
the parent class function in the child class.
Customization via Inheritance
• Inheritance uses the attribute definition tree for namespaces
• Any object.attr is searched for up the namespace tree. The lower definitions override the higher ones
• Attribute tree construction:
• Instance → assignments to ‘self’ attributes
• Class → statements (assignments) in class statements
• Superclasses → classes listed in parenthesis in header
• Specializing inherited methods
• Subclasses may inherit, replace, extend, or provide
• Direct superclass methods can be called
Composition Classes
• Inheritance models what is called an is a relationship. This means that when you have a
Derived class that inherits from a Base class, you created a relationship where Derived is a
specialized version of Base.
• Composition is a concept that models a has a relationship. It enables creating complex types
by combining objects of other types. This means that a class Composite can contain an object of
another class Component. This relationship means that a Composite has a Component.

• 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.

You might also like