Python OOPS
Classes:
It is the means of bundling data and functionality together.
It is basically the collection of objects.
These are created by the keyword 'class'.
Attributes are variables, and they are always public. These can be accessed by using dot(.)
Objects:
- It has the state and behaviour. State represents the attributes/properties while Behaviour represents the methods of an object.
'self' parameter:
it refers to the current instance of the class, and is used to access variables that belong to the class.
It can be named with any name and make sure it should be the first parameter in any function inside the class.
'self' Constructor:
when class instance is created, the constructor with name __init__() is called.
Use the __init__() to assign values to object properties.
Inheritance:
Allows to inherit all the properties and methods from another class. It is basically a parent - child relationship, child(Derived) class inheriting from parent's(Base) class.
When child class is created and constructor(__init__) is created it overrides the parent's __init__, it will no longer inherit the parent's class __init__ and in order to use the parent's class properties and methods then use 'super()' keyword.
Types of Inheritance:
Single Inheritance: Single-level inheritance enables a derived class to inherit characteristics from a single-parent class.
Multilevel Inheritance: Multi-level inheritance enables a derived class to inherit properties from an immediate parent class which in turn inherits properties from his parent class.
Hierarchical Inheritance: Hierarchical-level inheritance enables more than one derived class to inherit properties from a parent class.
Multiple Inheritance: Multiple-level inheritance enables one derived class to inherit properties from more than one base class.
Polymorphism:
'Poly' means Many, so it means having many forms.
Example: len() can be used in different ways, like for strings, it returns number of characters. For Tuple, it returns number of items.
It's often used in class methods where in different classes, the same method name can be used.
Encapsulation :
It is the process of binding data and members together. Example: Class
In other programming languages, we have access modifiers (private, public, protected) to specify type of the access. But in Python there is no provision to specify the type of the access, by default, all the methods and variables are public.
From above example, if you observe the name and age are accessed from outside the class. I order to make them private, use double underscore(__) in front of the variable.
- Now, after setting to private variable and if it is accessed outside the class, it is throwing an error.
Abstraction:
Hiding complex details and providing the necessary details to the user.
This can be achieved by creating Abstract Classes.
To create abstract class, it must inherit the ABC class that is defined in ABC module(available in Python's standard library). The class must have at least one abstract method, and it cannot be called directly but can be overridden. It must be decorated with @abstractmethod decorator.(Refer example above)