Python OOP (Object-Oriented Programming)

Like other programming languages, python also will support object-oriented programming (OOP) to create reusable code.

 

Following are some object-oriented programming concepts in python to improve code efficiency by creating reusable code.

Class in Python

In python, the class comprises data members such as constructors, properties, attributes, etc., and member functions.

Create a Class

To create a class in python, you need to use the class keyword as shown below.

 

class SampleClass:
   '''Class Creation'''
   properties
   functions
   ....
   ....

To learn more about classes in python, refer to python classes with examples.

Object in Python

As discussed, the object is an instance of a class to access the defined data members. The object will act as a blueprint of a class.

 

We can create the object of the above user class as shown below.

 

u1 = user()

After creating the object for the user class, you can access the class properties as shown below.

 

u1 = user()
print(u1.id)
print(u1.name)
u1.userdetails()

To learn more about objects in python, refer to python objects with examples.

Python Inheritance

In python, inheritance is the main concept of Object-Oriented Programming (OOP), and it is helpful to inherit all the properties and methods from one class to another class.

 

To implement the inheritance process in python, you need to define the child class with parentheses ( ) by including the base class.

 

Following is the syntax of implementing the inheritance in python.

 

class Base_Class:
   #Base/Parent Class Statements

class Child_Class(Base_Class):
   #Child/Derived Class Statements

To learn more about inheritance in python, refer to python inheritance with examples.

Python Access Modifiers

Like other programming languages such as C#, JAVA, and C++, we don't have access modifiers like public, private, and protected in python to define class member's access level restrictions.

 

By default, all the python class members (variablesmethods, etc.) are public. To define the class variables or methods as private/protected, you need to prefix the single or double underscore (_) to the variable/method name.

 

To learn more about access modifiers in python, refer to python access modifiers with examples.

Python Encapsulation

In python, encapsulation binds the data members such as variables, properties, etc., into a single unit.

 

In python, encapsulation is helpful to prevent the accidental modification of data. For example, the class you will create with required variables and functions will not allow you to modify the class members directly. Instead, you need to create an object to access or change those class members.

 

To learn more about encapsulation in python, refer to python encapsulation with examples.

Python Abstract Classes

In python, if you create a class with one or more abstract methods, we will call it an abstract class. Generally, the abstract classes are same as regular classes, but the only difference is the abstract classes will have one or more abstract methods.

 

Python won’t provide direct support to the abstract classes. To implement abstract classes in python, you need to import the ABC (Abstract Base Classes) module and create the abstract methods in the base class.

 

To learn more about abstract classes in python, refer to python abstract classes with examples.

Python Polymorphism

In python, polymorphism is the main concept of object-oriented programming, and it will provide an ability to take more than one form.

 

The polymorphism will provide an ability to perform different tasks with a single type entity as shown below.

 

print("Length of String: {}".format(len("Welcome to Tutlane")))
print("No. of Items in List: {}".format(len(["Suresh", "Rohini", "Trishi", "Hanshu"])))
print("No. of Keys in Dict: {}".format(len({"Name": "Suresh", "Location": "Hyderabad"})))

To learn more about polymorphism in python, refer to python polymorphism with examples.

Method Overloading in Python

Like other programming languages, method overloading will not support in python. If you try to overload a method in python, it won’t raise any compile-time errors. Instead, it will consider using the last defined method.

 

Following is the example of creating the class with different methods with same but with different parameters in python.

 

class Calculate:
   def add(self, a, b):
      print("a + b = {}".format(a + b))

   def add(self, a, b, c):
      print("a + b + c = {}".format(a + b + c))

c1 = Calculate()
c1.add(10, 20, 30)
c1.add(10,20)

To learn more about method overloading in python, refer to python method overloading with examples.

Method Overriding in Python

In python, method overriding means overriding the base class method in child class by creating the method with same name and signature to perform different operations.

 

Following is an example of implementing the method overriding in python.

 

#Base Class
class User:

   def getinfo(self):
      print("Learn Python")

#Derived Class
class Employees(User):

   def getinfo(self):
      print("Welcome to Tutlane")

e1 = Employees()
e1.getinfo()

 To learn more about method overriding in python, refer to python method overriding with examples.