Course Code: CSE 0613 2203B
Course Title: Introduction to Programming with
Python
Md. Shymon Islam
Lecturer
Department of Computer Science and Engineering
Shahjalal University of Science and Technology, Sylhet
Python OOP
• OOP stands for Object-Oriented Programming.
• Python is an object-oriented language, allowing you to structure your
code using classes and objects for better organization and reusability.
Advantages of OOP
• Provides a clear structure to programs
• Makes code easier to maintain, reuse, and debug
• Helps keep your code DRY (Don't Repeat Yourself)
• Allows you to build reusable applications with less code
What are Classes and Objects?
• Classes and objects are the two core concepts in object-oriented
programming.
• A class defines what an object should look like, and an object is created
based on that class.
Class Objects
Fruit Apple, Banana, Mango
Car Volvo, Audi, Toyota
Python Classes/Objects
• 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.
Create a Class
• To create a class, use the keyword class.
Example: Create a class named MyClass, with a property named x.
Create Object
Example: Create an object named p1, and print the value of x.
The __init__() Method
• The examples above are classes and objects in their simplest form, and
are not really useful in real life applications.
• To understand the meaning of classes we have to understand the built-in
__init__() method.
• All classes have a method called __init__() which is always executed
when the class is being initiated.
• Use the __init__() method to assign values to object properties, or other
operations that are necessary to do when the object is being created.
The __init__() Method
Example:
Create Methods
You can create your own methods inside objects. Methods in objects are
functions that belong to the object.
The self Parameter
• The self parameter is a reference to the current instance of the class, and
is used to access variables that belong 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.
The self Parameter
Example:
Modify Object Properties
You can modify properties on objects like this
Delete Object Properties
You can delete properties on objects by using the del keyword.
Delete Objects
You can delete objects by using the del keyword.
The pass Statement
class definitions cannot be empty, but if you for some reason have a class
definition with no content, put in the pass statement to avoid getting an
error.
Python 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.
Create a Parent Class
• Any class can be a
parent class, so
the syntax is the
same as creating
any other class.
• Create a class
named Person,
with firstname
and lastname
properties, and a
printname
method.
Create a Child Class
• To create a class that inherits the functionality from another class, send
the parent class as a parameter when creating the child class.
• Create a class named Student, which will inherit the properties and
methods from the Person class.
Child Class Makes Reuse
• Use the Student class (child class), to create an object, and then
execute the printname method.
Add the __init__() Function
• So far we have created a child class that inherits the properties and
methods from its parent.
• We want to add the __init__() function to the child class (instead of the
pass keyword.
• The __init__() function is called automatically every time the class is
being used to create a new object.
Add the __init__() Function
• When you add the __init__() function, the child class will no longer
inherit the parent’s __init__() function.
• The child’s __init__() function overrides the inheritance of the parent’s
__init__() function.
• To keep the inheritance of the parent’s __init__() function, add a call to
the parent’s __init__() function.
Use the super() Function
• Python also has a super() function, that will make the child class
inherit all the methods and properties from its parent.
Add Properties
• Add a property called graduationyear to the Student class.
Add Properties (Example)
• Add a property called graduationyear to the Student class.
Add Methods
• Add a method called welcome to the Student class.
Python Polymorphism
• The word “polymorphism” means “many forms”, and in
programming it refers to methods/functions/operators with the same
name that can be executed on many objects or classes.
OOP polymorphism can be of types-
• Class Polymorphism
• Inheritance Class Polymorphism
Class Polymorphism
• Polymorphism is often used in Class methods, where we can have
multiple classes with the same method name.
• For example, say we have three classes: Car, Boat, and Plane and they
all have a method called move().
Method Overloading
• A single class has multiple methods with the same name but
different parameter lists.
• If you define two methods with the same name in one class, the last
one overwrites the previous one.
• Python does not support the traditional concept of method
overloading.
Class
Polymorphism
Example →→→→→→
Inheritance Class Polymorphism
• What about classes with child classes with the same name? Can we use
polymorphism there?
• Yes. If we use the previous example and make a parent class called
Vehicle, and make Car, Boat, Plane child classes of Vehicle, the child
classes inherits the Vehicle methods, but can override them.
Inheritance Class
Polymorphism
• Child classes inherits the properties and
methods from the parent class.
• In the example above you can see that the
Car class is empty, but it inherits brand,
model and move() from Vehicle.
• The Boat and Plane classes also inherit
brand, model, and move() from Vehicle,
but they both override the move() method.
• Because of polymorphism we can execute
the same method for all classes.