Unit-IV-OOP-in-Python(NEP)
Unit-IV-OOP-in-Python(NEP)
(23DCE2101)
• POP likes building with LEGOs. Each LEGO brick represents a simple instruction.
By snapping them together in a specific order, you create a functioning program.
In POP, this means breaking down tasks into clear, step-by-step instructions that
the computer executes sequentially.
Procedural Programming (POP) is a structured programming approach.
Understanding Object-Oriented Programming (OOP)
Object-Oriented Programming
Feature Procedural Programming (POP)
(OOP)
Focus Procedures (functions) Objects (data + behavior)
Program Structure Sequence of functions Objects interacting with each other
Data Organization Global/local variables, function arguments Encapsulated within objects
High through inheritance and
Code Reusability Limited, prone to duplication
polymorphism
Improved due to encapsulation and
Maintainability Challenging for large projects
inheritance
Complex projects, code reusability,
Suitability Simple tasks, smaller projects, education
real-world modelling
Example Languages C, FORTRAN, Pascal C++, Java, Python, C#
POP vs. OOP: Which to Choose?
• Consider POP for: Simple, well-defined tasks, Smaller projects, Educational
purposes
• Consider OOP for: Complex projects, Code reusability, Large-scale
development
• Hybrid Approaches: In some cases, you might consider a hybrid approach
that combines elements of both POP and OOP. Here's when it might be
beneficial:
• Large projects with well-defined subtasks: If your project has a complex
overall structure but includes well-defined subtasks, you can leverage OOP for
the main structure and utilize POP for simpler subtasks within the objects.
• Phased development: When starting a project with a less clear scope, you
might begin with a simpler POP approach for initial development and then
gradually migrate towards OOP as requirements solidify and the project grows
in complexity.
OOP vs Procedural/Structured Programming
Procedural Oriented Programming Object-Oriented Programming
In procedural programming, the program is In object-oriented programming, the program is
divided into small parts called functions. divided into small parts called objects.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any Object-oriented programming provides data
proper way of hiding data so it is less secure. hiding so it is more secure.
Procedural programming uses the concept of Object-oriented programming uses the concept
procedure abstraction. of data abstraction.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.
Classes
• Creating classes:
Syntax:
class Classname:
‘optional class documentation string’
# list of python class variables
# python class constructor
# python class method definitions
Example:
class Car:
pass
• In a class we can define variables, functions, etc. While writing any function
in a class we have to pass at least one argument that is called ‘self
parameter’.
• The self parameter is a reference to the class itself and is used to access
variables that belongs to the class. It does not have to be named self, we
can call it whatever we like, but it has to be the first parameter of any
function in the class.
• We can write a class on interactive interpreter or in .py file.
class student:
def display(self):
print(“Hello Python”)
• In python programming, self is default variable that contains the address of
the instance of the current class. So we can use self to refer to all the
instance variables and instance methods.
Objects and creating classes
• Example:
s1=student()
s1.display()
• Program for demonstration:
class student:
def display(self):
print(“Hello Python”)
s1=student()
s1.display()
Output:
Hello Python
• Class with get and put method.
Program: put-get.py
• Destructor: A class can define a method called a destructor with the help
of __del__(). This method is invoked automatically when the
instance(object) is about to be destroyed.
• It is mostly used to clean up any non-memory resources used by an
instance(object).
• Program: destructor.py
Built-in class attributes
Python offers many tools and features to simplify software
development. Built-in class attributes are the key features that enable
developers to provide important information about classes and their
models. These objects act as hidden gems in the Python language,
providing insight into the structure and behavior of objects.
In Python, built-in class attributes are properties associated with a class
itself, rather than its instances. These attributes provide metadata
about the class and are accessible.
Every Python class keeps following built-in attributes and they can be
accessed using dot operator like any other attribute:
• __dict__: It displays the dictionary in which the class’s namespace is stored.
• __name__: It displays the name of the class.
• __bases__: It displays the tuple that contains the base classes, possibly
empty. It displays them in the order in which they occur in the base class list.
• __doc__: It displays the documentation string of the class. It displays none if
the docstring isn’t given.
• __module__: It displays the name of the module in which the class is defined.
Generally, the value of this attribute is “__main__” in interactive mode.
• Program: builtin-attributes.py
Method Overloading
• It is the ability to define the method with the same name but with the different
number of arguments and data types to perform different tasks.
• Python does not support method overloading, i.e. it is not possible to define
more than one method with the same name in a class in Python. This is because
method arguments in Python do not have a type. A method accepting one
argument can be called with an integer value, a string or a double, etc.
• Ex: class demo:
def method(self, a):
print(a)
Output:
50
obj = demo()
Minakshi
obj.method(50)
100.3
obj.method(‘Minakshi’)
obj.method(100.3)
Using default arguments (method overloading)
• It is possible to provide default values to method arguments while
defining a method. If method arguments are supplied default values,
then it is not mandatory to supply those arguments while calling
method as shown in the program – defaultarguments.py
Inheritance: Extending a class
• The mechanism of deriving a new class from an old one is called inheritance. The
old class is called as base class or super class or parent class and the new one is
called the subclass or derived class or child class.
• The inheritance allows the subclasses to inherit all the variables and methods of
their parent classes hence acquiring the properties of parent class. (Reusabaility)
• Defining a subclass
• Syntax:
class A:
# attributes of class A
class B(A):
# class B inheriting the properties of class A
# more class B properties
Single Inheritance
• Syntax:
class A:
# attributes of class A
class B(A):
# class B inheriting the properties of class A
# more class B properties
Programs: singleinheritance.py, singleinheritance-constructor.py
Multilevel Inheritance
• Syntax:
class A:
# attributes of class A
class B(A):
# class B inheriting the properties of class A
# more class B properties
class C(B):
# class C inheriting the properties of class B
# more class C properties
Program: Multilevelinheritance.py
Multiple Inheritance
• Syntax:
class A:
# attributes of class A
class B:
# Attributes of class B
class C(A, B):
# class C inheriting the properties of class A and class B
# more class C properties
Program: Multiple-inheritance.py
Hierarchical Inheritance
• Syntax:
class A:
# attributes of class A
class B(A):
# class B inheriting the properties of class A
# Attributes of class B
class C(A):
# class C inheriting the properties of class A
# more class C properties
Program: hierarchical-inheritance.py
Method Overriding
• Method Overriding is the ability of a class to change the implementation
(behaviour) of a method provided by one of its base class. It is thus a strict part of
the inheritance mechanism.
• Program: method-overriding.py
• In inheritance, delegation occurs automatically, but if a method is overridden the
implementation of the parent is not considered at all. So, if you want to run the
implementation of one or more of the ancestors of your class, you have to call
them explicitly.
• Using super() method: The super() method gives you access to methods in a
superclass that inherits from it.
• The super() method also returns a temporary object of the superclass that then
allows you to call that superclass’s methods.
• Program: method-overriding-super().py