Python Object-Oriented Programming (Oop) - Pynative
Python Object-Oriented Programming (Oop) - Pynative
• Protected
members are
accessible within
the class and also
available to its
sub-classes. To
define a protected
member, prefix
the member name
with a single
underscore _.
Getters and Setters in Python
• To implement proper encapsulation in Python, we need to
use setters and getters.
• The primary purpose of using getters and setters in object-
oriented programs is to ensure data encapsulation. Use the
getter method to access data members and the setter
methods to modify the data members.
• In Python, private variables are not hidden fields like in
other programming languages. The getters and setters
methods are often used when:
• When we want to avoid direct access to private variables
• To add validation logic for setting a value
Advantages of Encapsulation
• Security: The main advantage of using encapsulation is the security of
the data. Encapsulation protects an object from unauthorized access. It
allows private and protected access levels to prevent accidental data
modification.
• Data Hiding: The user would not be knowing what is going on behind
the scene. They would only be knowing that to modify a data member,
call the setter method. To read a data member, call the getter method.
What these setter and getter methods are doing is hidden from them.
• Simplicity: It simplifies the maintenance of the application by keeping
classes separated and preventing them from tightly coupling with each
other.
• Aesthetics: Bundling data and methods within a class makes code
more readable and maintainable
Inheritance in Python
• The process of inheriting the properties
of the parent class into a child class is
called inheritance. The existing class is
called a base class or parent class and the
new class is called a subclass or child class or
derived class.
• In Object-oriented programming, inheritance is
an important aspect. The main purpose of
inheritance is the reusability of code because
we can use the existing class to create a new
class instead of creating it from scratch.
• In inheritance, the child class acquires all the
data members, properties, and functions from
the parent class. Also, a child class can also
provide its specific implementation to the
methods of the parent class.
Types Of Inheritance
In Python, based upon the number of child and parent
classes involved, there are five types of inheritance.
The type of inheritance are listed below:
1.Single inheritance
2.Multiple Inheritance
3.Multilevel inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance
Single Inheritance
• In single inheritance, a child class inherits from a single-
parent class. Here is one child class and one parent
class.
Multiple Inheritance
• In multiple inheritance, one child class can inherit from
multiple parent classes. So here is one child class and
multiple parent classes
Multilevel inheritance
• In multilevel inheritance, a
class inherits from a child
class or derived class.
Suppose three classes A, B,
C. A is the superclass, B is
the child class of A, C is the
child class of B. In other
words, we can say a chain
of classes is called
multilevel inheritance.
Hierarchical Inheritance
• In Hierarchical inheritance, more than one child class is
derived from a single parent class. In other words, we
can say one parent class and multiple child classes.
Python super() function
Polymorphism in Built-in
function len()
The built-in function len() calculates the length of
an object depending upon its type. If an object is
a string, it returns the count of characters, and If
an object is a list, it returns the count of items in
a list.
The len() method treats an object as per its class
type.
Polymorphism With Inheritance