OOPs in Python stand for Object-oriented programming aims to implement real-world entities like inheritance, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
- Class
- Object
- Method
- Inheritance
- Polymorphism
- Data Abstraction
- Encapsulation
Class
Class is nothing but combination of data and method related with a things. So we can say that class is use for encapsulating data and method together in one unit. Class is the logical structure to create the objects. It is the user define data type.
Object
Object is nothing but instance of class. It we want to use the class data method then we have to create object.
Example of Object and class in Pythonclass key: x = ("basic engineer") a = key() print(a.x)
Let’s run the code
Inheritance
Inheritance in Java is the way of code reusability this provides the facility to establish a relation between two classes as parent-child or base-derived. In this relation base will provides its non-private data and functions to the derived class.
- Subclass /Child – This class that inherits from another class.
- Superclass /Parent – This class being inherited from.
Example of Inheritance in Pythonclass book: def read(self): print("HTML") class notebook(book): def write(self): print("HTML notebook") d = notebook() d.write() d.read()
Let’s run the code
Encapsulation
Encapsulation is a process of wrapping code and data together into a single unit, it is know as Encapsulation. It is refers to the practice of hiding the internal details of an object from the outside world and providing a public interface for interacting with the object. In Python, encapsulation is achieved through the use of private and protected methods and variables.
Example of encapsulation
A Example of encapsulationclass BankAccount: def __init__(self, balance): self.__balance = balance def deposit(self, amount): self.__balance += amount return self.__balance def withdraw(self, amount): if amount > self.__balance: return "Insufficient balance" self.__balance -= amount return self.__balance def get_balance(self): return self.__balance def __str__(self): return f"Balance: {self.__balance}" my_account = BankAccount(1500) print(my_account.get_balance()) print(my_account.deposit(1500)) print(my_account.withdraw(3000)) print(my_account.withdraw(700)) print(my_account)
Example of encapsulation
Polymorphism
Polymorphism is derive from two Greek word :- poly and morphs. The word poly means ‘many’ and morphs means ‘forms’. Polymorphism is One thing with many forms.
Example of Polymorphism in Pythonclass X(): def show(self): print("HTML") class Y(): def show(self): print("Python") # Driver's code x = X() x.show() y = Y() y.show()
Let’s run the code
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about Python Wikipedia please click here .
Stay Connected Stay Safe, Thank you
0 Comments