Object-Oriented Pattern
The object oriented pattern is the most common type of design pattern, and it can be found almost every high-level programming language. In this method, we create an object and particular class and use the class functionality using that defined class’s object. The oops concepts consist of the following component.
- Class
- Object
- Constructor
- Inheritance
- Encapsulation
- Polymorphism
We will discuss the above component in brief. To understand oops concept in detail, visit our Python Object-oriented tutorial.
Class
Class is the blueprint of the object; it contains methods and constructors. We use the class keyword to define the class. A Python class will look-like as it follows.
The class can contain the special attributes that begin with the double underscore (__). For example – __init__() is a non-parameterized constructor.
Object
The object is used to access the class attributes. We can create the class object as follows.
Output:
The salary is 20000
Constructors
Constructors are special methods that automatically call whenever a new object of that class is instantiated. They are started with the double underscore __ and called special functions. Constructor is defined as __int__() function. It is used to initialize all the variables.
Example –
Output:
The employee details: Sudhir 25
Inheritance
Inheritance is the process of derived the property of one class to another class. The class that derived another class’s property is known as subclass or child class and class that derived by another class is called the parent class or base class. Inheritance provides the reusability of the code.
Let’s understand the following example.
Example –
Output:
Bird Speaking Dog Barking
The self Parameter
The self works in Python same as the “this” in C++. It refers to the current class of function, and we can access data in the class function. It works as the variable but won’t invoke data.
Example –
Output:
Mathew Mathew
Encapsulation
Unlike C++, Python doesn’t support keywords like public, protected, and private to define data accessibility. By default, all data in Python is public. But it provides the facility to define any method or variable private. We can use the “__” to hide its property in front of the variable or method. Let’s understand the following example –
Output:
Traceback (most recent call last): File "", line 10, in AttributeError: 'Employee' object has no attribute 'name'
There is a special syntax that can use to access the private data or method in Python. The syntax is actually its change name. Let’s understand the following example.
Example –
Output:
Mathew 25
In the above code, we have defined the variable age as a private member of the class. To access it, we have used the special syntax using the class object./p>
Polymorphism
Polymorphism is one of the most important aspects of oops concepts. Python is a dynamic-typed language and tracks the variable types. Polymorphism is used to achieve many things in the same way.
Example –
Output:
30 tutoraspire20
As we can see in the above code, the ‘add’ operation is supported not only by the integer but also the string. This is a simple example of polymorphism.
Many operations and functions in Python are polymorphic. So as long as we use the polymorphic operators and functions, polymorphism will exist even if we don’t have this purpose.
Advantages of Object-Oriented Pattern
Below are a few important advantages of the Oops design pattern.
- It supports the dynamic-typed binding, which means an instance of a class can be changed dynamically at runtime.
- Python supports the multiple inheritances and operator overloading with an advanced definition that some OOPs language don’t have.
- Python oops concept doesn’t support the private, public and protected keyword because it creates some complexity in the syntax. As Python’s inventor, Guido Van Rossum, said, “Abundant syntax brings more burden than help.” We can reuse the code using the inheritance.
- Polymorphism provides flexibility in the program. Using polymorphism, we can create the same method in the parent class, and it would work in all child classes.
- It provides effective problem-solving. OOPs, the concept allows us to break-down the code into bite-size problems that we can then solve.
Disadvantages of Object-Oriented Pattern
The Python OOPs pattern also has some demerits. These demerits are given below.
- It is quite complex to create programs based on the interaction of objects.
- It will take time to learn for some people.
- Some oops programs are larger; it will also take much time for execution.