Python OOPS
Python OOPS
Python OOPS
In Python, object-oriented Programming (OOPs) is a programming paradigm that uses objects and classes in
programming. It aims to implement real-world en es like inheritance, polymorphisms, encapsula on, etc. in the
programming. The main concept of OOPs is to bind the data and the func ons that work on that together as a single unit
so that no other part of the code can access this data.
Python Class
A class is a collec on of objects. A class contains the blueprints or the prototype from which the objects are being
created. It is a logical en ty that contains some a ributes and methods.
Python Objects
The object is an en ty that has a state and behavior associated with it. It may be any real-world object like a mouse,
keyboard, chair, table, pen, etc. Integers, strings, floa ng-point numbers, even arrays, and dic onaries, are all objects.
More specifically, any single integer or any single string is an object. The number 12 is an object, the string “Hello, world”
is an object, a list is an object that can hold other objects, and so on. You’ve been using objects all along and may not
even realize it.
State: It is represented by the a ributes of an object. It also reflects the proper es of an object.
Behavior: It is represented by the methods of an object. It also reflects the response of an object to other
objects.
Iden ty: It gives a unique name to an object and enables one object to interact with other objects.
To understand the state, behavior, and iden ty let us take the example of the class dog (explained above).
State or A ributes can be considered as the breed, age, or color of the dog.
Python Polymorphism
Polymorphism simply means having many forms. For example, we need to determine if the given species of birds fly or
not, using polymorphism we can do this using a single func on.
Polymorphism in Python
This code demonstrates the concept of inheritance and method overriding in Python classes. It shows how subclasses
can override methods defined in their parent class to provide specific behavior while s ll inheri ng other methods from
the parent class.
Python Encapsula on
Encapsula on is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of
wrapping data and the methods that work on data within one unit. This puts restric ons on accessing variables and
methods directly and can prevent the accidental modifica on of data. To prevent accidental change, an object’s variable
can only be changed by an object’s method. Those types of variables are known as private variables.
A class is an example of encapsula on as it encapsulates all the data that is member func ons, variables, etc.
Python Inheritance
Inheritance is the capability of one class to derive or inherit the proper es from another class. The class that derives
proper es is called the derived class or child class and the class from which the proper es are being derived is called the
base class or parent class. The benefits of inheritance are:
It provides the reusability of a code. We don’t have to write the same code again and again. Also, it allows us to
add more features to a class without modifying it.
It is transi ve in nature, which means that if class B inherits from another class A, then all the subclasses of B
would automa cally inherit from class A.
Types of Inheritance
Single Inheritance: Single-level inheritance enables a derived class to inherit characteris cs from a single-parent
class.
Mul level Inheritance: Mul -level inheritance enables a derived class to inherit proper es from an immediate
parent class which in turn inherits proper es from his parent class.
Hierarchical Inheritance: Hierarchical-level inheritance enables more than one derived class to inherit proper es
from a parent class.
Mul ple Inheritance: Mul ple-level inheritance enables one derived class to inherit proper es from more than
one base class.
Data Abstrac on
It hides unnecessary code details from the user. Also, when we do not want to give out sensi ve parts of our code
implementa on and this is where data abstrac on came.