Python is the most demanding language of the era, used for Web Applications, Machine Learning, Data Science, and more. Object-Oriented Programming as a discipline has gained a universal following among developers. Creating Python classes and the objects are the basic and fundamental parts of OOP.
It includes concepts like Class, Object, Inheritance, Polymorphism, Abstraction and Encapsulation.
What is OOP ?
OOP deals with creating object of the class that contains data and methods. Each object represents a new logic and data.
What is a Class ?
A class is a blueprint of Object, that defines how an object will behave. It consists of member functions and member variables, that can be accessed by creating an object of the class. If you are creating a class for Employee the member functions can be employee_name, employee_salary etc.
In the above example, we’ve created a class named Car.
Creating an object :
Object is an instance of class, that has state and behaviour. It can access the data and functions defined under the class.
Output: Car Name: Honda City Car Year: 2018 {'name': 'Honda City', 'year': 2018} Explanation: obj is the object of class car, obj can access the function declared in the class. The method init is the constructor of class, that sets the variables when the object is created. The word dict returns the dictonary that prints all the values against the object.
Object-Oriented Programming :
1. Inheritance
We always comes accross someone saying, ‘You look exactly to your father or mother’ and this is due to inheriting the similarities from parents.
The same thing is followed in inheritance, we have a parent class and a derived class that inherits the properties of parent class.
Output: Honda City 6 Explanation: We've created a Parent class Car and a Child class SportsCar. Have you noticed that the variables name, model aren't initilized in the child class instead its done in the base class by using super() method and its accessable to child class object.
2. Encapsulation
Encapsulation basically means storing data related to one entity in one class. Python doesn’t have access modifiers private, public, and protected like C++ or Java. A class shouldn’t be accessed directly from the object or inheritance. We have an underscore for it.
Output: Honda City Honda City Explanation: We've a class named Car, the member variable name is private and can't be accessed outside the class not even by it's object, so we have to write method getName that return the name of Car. We are doing a indirect access to private member via method.Note: You can use single or double underscore to initialize a private member of class.
3.Polymorphism
Polymorphism means using comman interface for different data types.
Example: A bird can fly irrespective of its type(color, shape, size).
Output: Parrot can Fly Sparrow can Fly
In the above program, we’ve two classes named Parrot and Sparrow, both has a comman method Fly after instantiating objects the method Comman_Interface is called and object is passed into it. The Fly method of both class work effectively.
4.Abstraction
Abstraction simply means defining a class for inheritance and the base class has abstract method declared.
Example: You got an OTP for online transaction, you can only see the OTP and hence how the OTP is generated is not known to us.This is Abstraction i.e. Showing the essential things and hiding the rest.Syntax from abc import ABC Class ClassName(ABC):
Output: Parrot can Fly Sparrow can Fly
Explanation: We’ve Abstract class Bird with Fly as abstract method. Class Parrot & Sparrow inherts Fly from base class and implements their own functionality as required.
Hope it clears your concepts of OOP related to Python, Practice it as much as you can.
Got a question for us ? Please mention it in the comments below and we’ll get back to you ASAP.
Doing great work bro
Thank You !
Very nice 👍
Thank you ma’am !