OOP in Python
OOP in Python
Objects do not share instance attributes. Instead, every object has its copy
of the instance attribute and is unique to each object.
All instances of a class share the class variables. However, unlike instance
variables, the value of a class variable is not varied from object to object.
Using the super() method we can invoke the parent class constructor from a
child class.
Encapsulation:
Encapsulation in Python describes the concept of bundling data
and methods within a single unit. So, for example, when you create a class,
it means you are implementing encapsulation. A class is an example of
encapsulation as it binds all the data members (instance variables) and
methods into a single unit.
Private members are accessible only within the class, and we can’t access
them directly from the class objects.
Output
Output:
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 _.
Protected data members are used when you implement inheritance and want to allow data
members access to only child classes.
Getters and Setters in Python
In Python, private variables are not hidden fields like in other programming
languages. The getters and setters methods are often used when:
For example, Jessa acts as an employee when she is at the office. However,
when she is at home, she acts like a wife. Also, she represents herself
differently in different places. Therefore, the same person takes different forms
as per the situation.
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.
Method overriding is useful when a parent class has multiple child classes, and
one of that child class wants to redefine the method. The other child classes
can use the parent class method. Due to this, we don’t need to modification the
parent class code.
Method Overloading
The process of calling the same method with different parameters is known as
method overloading. Python does not support method overloading. Python
considers only the latest defined method even if you overload the method.
Python will raise a TypeError if you overload the method.
Inheritance:
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
Class variable and Class Methods:
In Class, attributes can be defined into two parts: