You are on page 1of 18

Object Oriented Programming

Module 1 Subtopic 1

medium.com
LEARNING OBJECTIVES
At the end of this lesson, the students should
be able to:

1. Understand what is Object Oriented “Before software can


Python; be reusable it first has
2. Explain the fundamentals of Object to be usable.” – Ralph
Oriented Programming in Python; Johnson
3. Attempt to use the different
approaches in Object Oriented
Python Programming
Object Oriented Programming
 In Python, object-oriented Programming (OOPs) is a programming
paradigm that uses objects and classes in programming. It aims to
implement real-world entities like inheritance, polymorphisms,
encapsulation, etc. in the programming.
 The main concept of OOPs is to bind the data and the functions
that work on that together as a single unit so that no other part of
the code can access this data.
Object Oriented Programming
 One of the popular approaches to solve a programming problem is
by creating objects. This is known as Object-Oriented Programming
(OOP).
 An object has two characteristics:
1. attributes
2. behavior
Let's take an example:
- A parrot is an object, as it has the following properties:
- name, age, color as attributes
- singing, dancing as behavior
Main Concepts of OOP
 Class
 Objects
 Polymorphism
 Encapsulation
 Inheritance
Structure of OOP
1. Classes – are user-defined data types
that act as the blueprint for individual
objects, attributes and methods
2. Objects – are instances of a class
created with specifically defined data.
Objects can correspond to real-world
objects or an abstract entity. When
class is defined initially, the description
is the only object that is defined.
Structure of OOP
3. Methods - are functions that are defined inside a class that describe
the behaviors of an object. Each method contained in class definitions
starts with a reference to an instance object. Additionally, the
subroutines contained in an object are called instance methods.
Programmers use methods for reusability or keeping functionality
encapsulated inside one object at a time.
4. Attributes - are defined in the class template and represent the state
of an object. Objects will have data stored in the attributes field. Class
attributes belong to the class itself.
Class in Python
 A class is a collection of objects.
 A class contains the blueprints or the prototype from which the objects
are being created. It is a logical entity that contains some attributes and
methods.
 Classes are used to create user-defined data structures.
 Classes define functions called methods, which identify the behaviors and
actions that an object created from the class can perform with its data.
Some points in Python Class
 Classes are created by keyword class.
 Attributes are the variables that belong to a class.
 Attributes are always public and can be accessed using the dot
(.) operator. Eg.: Myclass.Myattribute
Define a Class Syntax
Define a Class

In this example, we defined a class named Book.


The __init__ special method, also known as Constructor, is used to initialize
the Book class with attributes such as title, quantity, author, and price.
The term self in the attributes refers to the corresponding instances
(objects).
Objects
This class can be instantiated (creating a copy of a class which inherits all
class variables and methods) to any number of objects. Three books are
instantiated in the following example code:

book1, book2, and book3 are distinct objects of the class Book.

A class instance with a defined set of properties is called an object. As a


result, the same class can be used to construct as many objects as needed.
Objects
If we will add the following codes, the output would be below:

The class and memory location of the objects are printed when they are
printed. We can't expect them to provide specific information on the
qualities, such as the title, author name, and so on. But we can use a
specific method called _ _repr_ _ (returns the object representation in
string format) to do this.
In Python, a special method is a defined function that starts and ends with
two underscores and is invoked automatically when certain conditions are
met.
Objects

Output:
Methods
 Methods are functions defined inside the body of a
class.
 They are used to define the behavior of an object.
Creating Methods in Python
The following adds an instance method called display() to the Person class:
Output:

To call an instance method, use the


dot notation.
Creating Methods in Python
The following adds an instance method called show() to the Student class:
Output:

To call an instance method, use the


dot notation.
References:
https://realpython.com/python3-object-oriented-programming/
https://www.programiz.com/python-programming/object-oriented-programming
https://www.programiz.com/python-programming/class
https://www.tutorialspoint.com/python/python_classes_objects.htm
https://pynative.com/online-python-code-editor-to-execute-python-code/

You might also like