0% found this document useful (0 votes)
5 views53 pages

Python OOP: Concepts and Examples

The document provides an overview of Object-Oriented Programming (OOP) in Python, explaining its principles such as encapsulation, inheritance, polymorphism, and abstraction. It discusses the importance of classes and objects, how to define them, and the benefits of using OOP, including code reusability and maintainability. Additionally, it covers specific concepts like class attributes, instance methods, and the use of abstract classes for data abstraction.

Uploaded by

Danish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views53 pages

Python OOP: Concepts and Examples

The document provides an overview of Object-Oriented Programming (OOP) in Python, explaining its principles such as encapsulation, inheritance, polymorphism, and abstraction. It discusses the importance of classes and objects, how to define them, and the benefits of using OOP, including code reusability and maintainability. Additionally, it covers specific concepts like class attributes, instance methods, and the use of abstract classes for data abstraction.

Uploaded by

Danish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to python

OOP
OOPs in Python
 OOP stands for Object Oriented Language.
OOP is a programming paradigm (which is style
or approach to programming).
OOP is centered around the objects.
Now in an object-oriented language, this one
large program will instead be split apart into self
contained objects, almost like having several
mini-programs, each object representing a
different part of the application.
OOPs in Python
 And each object contains its own data and its
own logic, and they communicate between
themselves.
These objects aren’t random. They represent the
way you talk and think about the problem you
are trying to solve in your real life.
 Example: Think of a class as a blueprint for a car. This
blueprint defines the attributes (like color, model, and
engine type) and behaviors (like drive, brake, and
honk). Each car object created from this blueprint will
have its own specific attributes and can perform the
behaviors defined by the class.
OOPs in Python
Procedural programming is about writing
procedures or methods that perform operations
on the data, while object-oriented
programming is about creating objects that
contain both data and methods
OOPs in Python
Object-oriented programming has several advantages over
procedural programming:
 OOP is faster and easier to execute
 OOP provides a clear structure for the programs
 OOP helps to keep the code DRY "Don't Repeat Yourself",
and makes the code easier to maintain, modify and
debug.
 OOP makes it possible to create full reusable applications
with less code and shorter development time
 Tip: The "Don't Repeat Yourself" (DRY) principle is about
reducing the repetition of code. You should extract out the
codes that are common for the application, and place
them at a single place and reuse them instead of
OOPs in Python
 In object-oriented programming technique, we design a
program using objects and classes.
 Python is an object-oriented programming language.
 Everything in python is object, with its properties and
methods.
 It allows you to divide complex problems into smaller sets
by creating objects.
 An object is the physical as well as a logical entity,
whereas, a class is a logical entity only.
 All four core aspects of a generic OOP framework are
supported by Python's object-oriented programming
system: encapsulation, abstraction, inheritance, and
polymorphism.
OOPs in Python (Classes and Objects)
 A class is like a blueprint of a specific object.
 An object is any entity that has attributes and behaviors.
For example, a parrot is an object. It has
attributes - name, age, color, etc.
behavior - dancing, singing, etc
 Class is a logical entity.
 Object is a physical entity and logical enity.
OOPs in Python (Classes and Objects)
 A class is like a blueprint of a specific object.
 In the real world, every object has some color, shape, and
functionalities. For example, the luxury car Ferrari. Ferrari is
an object of the luxury car type. The luxury car is a class
that indicates some characteristics like speed, color,
shape, interior, etc. So any company that makes a car that
meets those requirements is an object of the luxury car
type. For example, every single car of BMW, Lamborghini,
Cadillac are an object of the class called 'LuxuryCar'. Here,
'LuxuryCar‘ is a class, and every single physical car is an
object of the luxury car class.
 A logical entity.
OOPs in Python (Classes and Objects)
 Classes and objects are the two main aspects of
object-oriented programming.
OOPs in Python (Classes and Objects)
OOPs in Python (Classes and Objects)
OOPs in Python (Classes and Objects)
 To create an object, you define a class first. And then, from the
class, you can create one or more objects.
Define a class
 To define a class, you use the class keyword followed by the class
name and a colon sign.
 To create an object from the Person class, you use the class name
followed by parentheses
OOPs in Python (Classes and Objects)
 Python is dynamic, it means you can add attributes to an
instance of a class dynamically at run time. Here we are adding
two attributes: name and age.
 You can see attributes as data stored within an object. While
class attributes are common to all instances created from
your class, instance attributes are unique to each instance.
OOPs in Python (Classes and Objects)
 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.
 The __init__special method, also known as a Constructor, is
used to initialize the class with attributes such as name and
age.
 The term self in the attributes refers to the corresponding
instances (objects).
OOPs in Python (Classes and Objects)
 When you create a Person object, Python automatically calls
the __init__ method to initialize the instance attributes. In
the __init__ method, the self is the instance of the Person class

 The person object now has the name and age attributes
OOPs in Python (Classes and Objects)
 To access an instance attribute, you use the dot notation.
 The following adds an instance method called greet() to the Person class
 To call an instance method, you also use the dot notation.
OOPs in Python (Classes and Objects)
Define class attribute
 Class attributes are variables that belong to the class itself, rather than to individual
instances of the class.
 Unlike instance attributes, class attributes are shared by all instances of the class.
They are helpful if you want to define class constants or variables that keep track
of the number of instances of a class.
 The following creates two instances of the Person class and shows the value of
the counter
OOPs in Python (Classes and Objects)
Lets take another example:
 Lets make a class named Book for a bookseller’s sales software
 This class can be instantiated to any number of objects. Three books are
instantiated in the following example code:
OOPs in Python (Classes and Objects)
 In above example 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__ to
do this.
 Provides a complete string representation of the object, useful for debugging and
logging.
OOPs in Python (Classes and Objects)
Accessing Attributes: You access an object’s attributes (variables) and method (functions)
using the dot notation.
OOPs in Python (Classes and Objects)
Lets take another example and create a class name speaker.
OOPs in Python (Classes and Objects)
In addition to class and instance attributes, classes can also have methods, known as
instance methods. Instance methods are functions defined within a class that operate on
instances of the class.
OOPs in Python (Classes and Objects)
static method
A static method is not bound to a class or any instances of the class. In Python, you use static
methods to group logically related functions in a class. To define a static method, you use
the @staticmethod decorator.
OOPs in Python (Classes and Objects)
static method
To call a static method, you use the ClassName.static_method_name()
OOPs in Python (Encapsulation)
 Encapsulation is one of the core concepts of OOP.
 It refers to the bundling of data (attributes) and methods within a
class.
 Encapsulation provides data protection and control over how the
code interacts with an object's internal state.
 Encapsulation is the process of preventing clients from accessing
certain properties, which can only be accessed through specific
methods.
 It prevents outer classes from accessing and changing attributes and
methods of a class. This also helps to achieve data hiding.
 You can achieve encapsulation in Python by defining private
attributes and methods within a class.
 private attributes and methods are prefixed with a single underscore
(_) or (__).
OOPs in Python (Encapsulation)
In Example person class we have two attributes name and age. When user access age directly it can enter –ve age
which is not true or manipulate other values. So make these two attributes private and access them via methods.
OOPs in Python (Encapsulation)
Lets recall the class Book now nake an prive attribute discount in class Book. We cant access privte attribute.
OOPs in Python (Encapsulation)
OOPs in Python (Encapsulation)
In the preceding example, the color and model attributes are private attributes of
the Speaker class.
OOPs in Python (Encapsulation)
OOPs in Python (Inheritance)
 Inheritance is one of the core concept of OOP.
 It allows classes to inherit attributes and methods from other classes or the ability to inherit methods
and/or characteristics from another class is known as inheritance.
 The class from which you inherits attributes and methods known as the parent or base class or super
class.
 The new class is called the child or derived class or sub class.
 Inheritance promotes code reuse by allowing the child class to inherit and extend the functionality of
the parent class.
 This helps in creating hierarchical relationships between classes and organizing code in a more
structured and logical manner.
 Python supports all type of inheritance : Single inheritance, multiple inheritance, multi-level inheritance,
hierarchical inheritance and hybrid inheritance
 In Python, a class inherits from another class using parentheses () and the name of the base class. There
is no special keyword for inheritance, but the syntax uses parentheses to indicate which class is being
inherited from. The super() function is often used to call methods from the parent class.
 Use super() to inherit parent class constructor. Or The super() function is used to call methods from
the parent class (such as the constructor __init__).
OOPs in Python (Single Inheritance)
 In single inheritance, a child class inherits from a single-parent class. Here is one child class
and one parent class.
OOPs in Python (Single Inheritance)
 In single inheritance, a child class inherits from a single-parent class. Here is one child class
and one parent class.
OOPs in Python (Single Inheritance)
OOPs in Python (Single Inheritance)
OOPs in Python (Multiple Inheritance)
OOPs in Python (Multiple Inheritance)
OOPs in Python (Multiple Inheritance)
OOPs in Python (Multi level Inheritance)
 Multi level inheritance is a type in which a class is derived from another class, which in turn is
derived from another class. In other words, it creates a chain of classes where each class
inherits from the one above it.
OOPs in Python (Multi level Inheritance)
OOPs in Python(Hierarchical Inheritance)
 Hierarchical inheritance is a type of inheritance where multiple classes inherit from a
single base class. This means that a single base class is shared by multiple derived classes.
Each derived class can have its own additional properties and methods, but they all
share the common attributes and behaviors provided by the base class.
OOPs in Python(Hierarchical
Inheritance)
OOPs in Python(Hierarchical
Inheritance)
OOPs in Python(Hierarchical
Inheritance)
OOPs in Python(Hierarchical Inheritance)
OOPs in Python (Polymorphism)
 Polymorphism is a very important concept in programming. It refers to the use of a single
type entity (method, operator or object) to represent different types in different scenarios.
 Builtin Function
OOPs in Python (Polymorphism)
 user-defined polymorphic functions:.
OOPs in Python (Polymorphism)
 Polymorphism with class methods: Python can use two different class types, in the same
way. We create a for loop that iterates through a tuple of objects. Then call the methods
without being concerned about which class type each object is. We assume that these
methods actually exist in each class.
OOPs in Python (Polymorphism)
 Polymorphism with Inheritance: Polymorphism lets us define methods in the child class that
have the same name as the methods in the parent class. In inheritance, the child class
inherits the methods from the parent class. However, it is possible to modify a method in a
child class that it has inherited from the parent class. This is particularly useful in cases where
the method inherited from the parent class doesn’t quite fit the child class. In such cases,
we re-implement the method in the child class. This process of re-implementing a method in
the child class is known as Method Overriding.
OOPs in Python (Polymorphism)
 Polymorphism with Inheritance:
OOPs in Python (Abstraction)
 Data abstraction is one of the most essential concepts of Python OOPs
 It hides irrelevant details from the user and show the details that are relevant to the users.
 A car has an accelerator, clutch, and break and we all know that pressing an accelerator
will increase the speed of the car and applying the brake can stop the car but we don’t
know the internal mechanism of the car and how these functionalities can work this detail
hiding is known as data abstraction.
 It enables programmers to hide complex implementation details while just showing users the
most crucial data and functions.
 This abstraction makes it easier to design modular and well-organized code, makes it
simpler to understand and maintain, promotes code reuse, and improves developer
collaboration.
OOPs in Python (Abstraction)
 In Python, we can achieve data abstraction by using abstract classes and abstract classes can be
created using abc (abstract base class) module and abstractmethod of abc module.
 bstract class is a class in which one or more abstract methods are defined. When a method is declared
inside the class without its implementation is known as abstract method.
 Abstract Method: In Python, abstract method feature is not a default feature. To create abstract
method and abstract classes we have to import the “ABC” and “abstractmethod” classes from abc
(Abstract Base Class) library.
 Abstract method of base class force its child class to write the implementation of the all abstract
methods defined in base class. If we do not implement the abstract methods of base class in the child
class then our code will give error.
 In the below code method_1 is a abstract method created using @abstractmethod decorator.

from abc import ABC, abstractmethod


class BaseClass(ABC):
@abstractmethod
def method_1(self):
#empty body
pass

You might also like