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.
We will discuss the above component in brief. To understand oops concept in detail, visit our Python Object-oriented tutorial.
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.
The object is used to access the class attributes. We can create the class object as follows.
The salary is 20000
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.
The employee details: Sudhir 25
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.
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.
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 -
Traceback (most recent call last): File "
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.
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 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.
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.
Disadvantages of Object-Oriented Pattern
The Python OOPs pattern also has some demerits. These demerits are given below.
Next TopicObserver Design Pattern