Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

OOP in Python

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including the distinction between instance and class variables, methods, encapsulation, and inheritance. It explains key principles such as constructor chaining, access modifiers, and the use of getters and setters for encapsulation. Additionally, it covers polymorphism, method overriding, and the types of inheritance available in Python.

Uploaded by

Anand Walandikar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

OOP in Python

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including the distinction between instance and class variables, methods, encapsulation, and inheritance. It explains key principles such as constructor chaining, access modifiers, and the use of getters and setters for encapsulation. Additionally, it covers polymorphism, method overriding, and the types of inheritance available in Python.

Uploaded by

Anand Walandikar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

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.

Accessing properties and assigning values

 An instance attribute can be accessed or modified by using the dot


notation: instance_name.attribute_name.
 A class variable is accessed or modified using the class name
Inside a Class, we can define the following two types of methods.

 Instance methods: Used to access or modify the object state. If we


use instance variables inside a method, such methods are called instance
methods. It must have a self parameter to refer to the current object.
 Class methods: Used to access or modify the class state. In method
implementation, if we use only class variables, then such type of methods
we should declare as a class method. The class method has
a cls parameter which refers to the class.
Constructor chaining
Constructor chaining is the process of calling one constructor from another
constructor. Constructor chaining is useful when you want to invoke multiple
constructors, one after another, by initializing only one instance.

In Python, constructor chaining is convenient when we are dealing


with inheritance. When an instance of a child class is initialized, the
constructors of all the parent classes are first invoked and then, in the end, the
constructor of the child class is invoked.

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.

Encapsulation is a way to restrict access to methods and variables from outside


of class. Whenever we are working with the class and dealing with sensitive
data, providing access to all variables used within the class is not a good
choice.

Access Modifiers in Python

Encapsulation can be achieved by declaring the data members and methods of


a class either as private or protected. But In Python, we don’t have direct
access modifiers like public, private, and protected. We can achieve this by
using single underscore and double underscores.
Private Member

We can protect variables in the class by marking them private. To define a


private variable add two underscores as a prefix at the start of a variable name.

Private members are accessible only within the class, and we can’t access
them directly from the class objects.

Output

AttributeError: 'Employee' object has no attribute '__salary'


We can access private members from outside of a class using the following two
approaches

 Create public method to access private members


 Use name mangling

Public method to access private members


Example: Access Private member outside of a class using an instance
method.

Output:

Name: Jessa Salary: 10000

Private members are accessible from inside of a class


Protected Member

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

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
Polymorphism:
Polymorphism in Python is the ability of an object to take many forms. In simple
words, polymorphism allows us to perform the same action in many different
ways.

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.

In polymorphism, a method can process objects differently depending on


the class type or data type. Let’s see simple examples to understand it
better.

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.


Method OverRiding:

Using method overriding polymorphism allows us to defines methods in the


child class that have the same name as the methods in the parent class.
This process of re-implementing the inherited method in the child
class is known as Method Overriding.

It is effective when we want to extend the functionality by altering the inherited


method. Or the method inherited from the parent class doesn’t fulfill the need
of a child class, so we need to re-implement the same method in the child class
in a different way.

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:

 Instance variables: If the value of a variable varies from object to object,


then such variables are called instance variables.
 Class Variables: A class variable is a variable that is declared inside of
class, but outside of any instance method or __init__() method.

Class variables are shared by all instances of a class. Unlike instance


variable, the value of a class variable is not varied from object to object,

school_name is the class variable

You might also like