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.

class key:
  x = ("basic engineer")

a = key()
print(a.x)
Example of Object and class in Python

Let’s run the code

Example of Object and class in Python
Example of Object and class in Python

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.
class book:  
    def read(self):  
        print("HTML")  
class notebook(book):  
    def write(self):  
        print("HTML notebook")  
d = notebook()  
d.write()  
d.read()  
Example of Inheritance in Python

Let’s run the code

Example of Inheritance in Python
Example of Inheritance in Python

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

class 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) 
A Example of encapsulation

Example of encapsulation

Example of Encapsulation in Python
Example of encapsulation in Python

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.

class X():
    def show(self):
        print("HTML")
         
class Y():
    def show(self):
        print("Python")
         
# Driver's code
x = X()
x.show()
y = Y()
y.show()
Example of Polymorphism in Python

Let’s run the code

Example of Polymorphism in Python
Example of Polymorphism in Python

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

Categories: Software

Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *