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

12 - OOP in Python

The document provides an overview of Object-Oriented Programming (OOP) concepts, including definitions of classes, objects, and attributes. It explains the importance of encapsulation, inheritance, polymorphism, and abstraction, along with practical examples and methods for creating and managing classes in Python. Additionally, it covers the use of the __init__ method, the concept of self, and memory management through garbage collection.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

12 - OOP in Python

The document provides an overview of Object-Oriented Programming (OOP) concepts, including definitions of classes, objects, and attributes. It explains the importance of encapsulation, inheritance, polymorphism, and abstraction, along with practical examples and methods for creating and managing classes in Python. Additionally, it covers the use of the __init__ method, the concept of self, and memory management through garbage collection.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

B.Sc. Hons.

Computing/Information System

ICT 1405 - Programming Methodologies


12 – Object Oriented Programming

<@Lanka Nippon BizTech Institute> 1


ACKNOWLEDGEMENT

Presented by : Ms. C. D. Padmaperuma


Ms. Hansika Samanthi

Contributors : Ms. Hansika Samanthi


Ms. C. D. Padmaperuma

Programming Methodologies | Exception Handling 2


REFERENCES

• Course Page
UOG 6: https://lms.lnbti.lk/course/view.php?id=851
UOG 11: https://lms.lnbti.lk/course/view.php?id=1306
• References available for the course
w3schools Python - https://www.w3schools.com/Python/
Real Python - https://realpython.com/

Programming Methodologies | Exception Handling 3


What is OOP?

• Object-oriented programming
is a programming paradigm.
• It provides a means of
structuring programs so that
properties and behaviors are
bundled into individual
objects.
Object

• An object is any entity that has attributes and behaviors.

Note:
Each object is an “instance” of
that “class” of object.
Each instance has its own
values for its attributes.
Ex: Different students have
different names
Class
Person
Attributes
• A class is a collection of Name
objects. Date of Birth
Address
• A class contains the
blueprints or the prototype Behaviors
from which the objects are
Walk
being created.
Eat
• It is a logical entity that Sleep
contains some attributes and
methods.
Class Object
(Blueprint of the object) (Instance of the class)
Importance of the classes

How can you keep track of the data for 100


students at the university?
Implementing a Class

• Classes are created by keyword class.


• Attributes are the variables that belong to a class.

Here,
• class name is Student.
• university is an attribute that belongs to the
class.
Empty Class

• In python, you can create empty classes using “pass” keyword.


Class Attributes Vs. Instance Attributes
• In Python, attributes can be associated with either a class or an instance of a
class.
Class Attribute Instance Attribute

Shared Across All Instances: Unique to Each Instance:


• Variables that are shared across all instances of a class. • Variables that belong to a specific instance of a class.
• Defined within the class but outside any instance methods. • Each instance can have different values for these attributes.
Accessed via Class or Instance: Defined in the __init__ Method:
• Access class attributes using the class name or through an • Defined inside the __init__ method using self, which refers
instance, to the specific instance.
• But they maintain the same value across all instances
unless explicitly overridden in an instance.
Class variable

Class Student
University =
LNBTI
Instance variables Name
Address
self
• Class methods must have an extra first parameter in the method
definition.
• We do not give a value for this parameter when we call the method,
Python provides it.
• If we have a method that takes no arguments, then we still have to have
one argument.
__init__ method
• The __init__ method is similar to constructors in C++ and Java.
• It is run as soon as an object of a class is instantiated.
• The method is useful to do any initialization you want to do with your
object.
Creating
Objects
Class Student
University =
LNBTI
Name
Address

Object S1
University = LNBTI
Name = Kamal
Address = Maharagama
Destroy Objects

• In Python, you don't typically need to manually destroy objects because Python
has a built-in garbage collector that automatically manages memory.

• However, you can remove references to an object, which allows the garbage
collector to clean it up when it's no longer needed.

• Two methods:

• Using ‘del’ keyword

• Removing all references


Destroy Objects
1. Using del keyword

• The del keyword deletes a reference to an object.


• If the object has no more references, it will be garbage-collected.
Destroy Objects
2. Removing all references
• If you simply remove all references to an object (e.g., setting the variable to
None or reassigning it), the object will be eligible for garbage collection.
OOP Concepts

Four fundamental concepts of Object-oriented programming as follows:


• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
Encapsulation
Bundling data and methods that operate on the data into a
OOP Concepts

single unit.

Class

__init__ method
Inheritance
A way to create a new class that inherits attributes and methods
OOP Concepts

from an existing 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.
Person
• Ex: Person, Student and Staff are three classes. Student and Staff
inherit attributes and methods from Person class. Person class is
the parent and StudentStudent Staff
and Staff are child classes.
Parent class
__init__ method
in Parent class

Child class

__init__ method
in Child class
Types
of
Inheritanc
e
Polymorphism
Having many forms.
OOP Concepts

• Allows objects of different classes to be treated as objects of a common


base class.
• Polymorphism is achieved through,
• Method overriding
• Method overloading

• Method overriding is the practice of defining a method in a subclass that


already exists in the parent class.
• Method overloading, on the other hand, is the ability to define multiple
methods in the same class with the same name but different parameters.
Parent class

Method
Overriding Child class

Overriding

Child class

Overriding
Method Overloading
Method overloading in Python can be done using the following methods.
• Default Arguments:
• Provide default values for parameters.
• Variable-Length Arguments:
• Use *args and **kwargs to accept a flexible number of arguments.
• Type Checking:
• Implement conditional logic based on argument types or counts.
• singledispatch:
• Use function dispatching based on argument types.
• Method Wrappers:
• Create methods that handle different argument scenarios and dispatch to
specific implementations.
Abstraction
OOP Concepts
Hiding the internal implementations of a process or
method from the user.

• In this way, the user knows what he is doing but not how the work is
being done.
• Ex: Car
Abstract methods are methods that
must be implemented by any
concrete subclass of the abstract
base class. Parent class
• ABC Class:
• ABC stands for Abstract Base
Class.
• It is a base class provided by
the abc module that you can
inherit from to create abstract Child class
base classes.
• @abstractmethod Decorator:
• Used to mark methods in an
abstract base class as
abstract.
END

You might also like