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.
In python, the class comprises data members such as constructors, properties, attributes, etc., and member functions.
To create a class in python, you need to use the
class keyword as shown below.
To learn more about classes in python, refer to python classes with examples.
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.
After creating the object for the user class, you can access the class properties as shown below.
To learn more about objects in python, refer to python objects with examples.
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.
To learn more about inheritance in python, refer to python inheritance with examples.
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 (variables, methods, 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.
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.
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.
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.
To learn more about polymorphism in python, refer to python polymorphism with examples.
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.
To learn more about method overloading in python, refer to python method overloading with examples.
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.
To learn more about method overriding in python, refer to python method overriding with examples.