OOPs Concept in Python

OOPs in Python: 

Object-oriented programming (OOPs) is a method of designing a program by binding there related properties and behaviours into individual objects. Here we will discuss, basics of object-oriented programming in Python.  

For instance, an object could represent a Student with properties like a name, age, class, marks, stream etc. 

Example:

  • Student->
    • Name 
    • Age 
    • roll number
    • stream
OOPs in Python

Principles of OOPs: 

  • Class 
  • Object
  • Method
  • Inheritance
  • Polymorphism
  • Data Abstraction
  • Encapsulation

Class: 

  • The class can be defined as a collection of objects. It is a logical entity that has some specific attributes and methods. 
  • We can also say that class is a blueprint of an object i.e. if any person is an object, class will have all the information about the structure or behaviour that person.  
  • OOPs, reduce the size of our code.
#python Program
#Rishikesh 
class Prepster
    def __init__(selfname , userid,password):
        self.name=name 
        self.userid=userid 
        self.password=password

Object: 

  • Object is a simple entity which posses some property or behaviour.
  • Everything in Python is an object, and almost everything has attributes and methods.
#python Program
#Rishikesh 
class Prepster
    def __init__(selfname , userid,password):
        self.name=name 
        self.userid=userid 
        self.password=password
 
ob1=Prepster(‘rishikesh’,‘Abc’,‘abc@123’##object 
print(ob1.name)

Output

rishikesh

Method: 

  • The method is a function that is associated with an object.
  • In Python, a method is not unique to class instances. Any object type can have methods.
#python Program
#Rishikesh 
class Prepster
    def __init__(selfname , userid,password):
        self.name=name 
        self.userid=userid 
        self.password=password
    def display(self): ##method of an class 
        print(self.name)
        print(self.userid)
        
ob1=Prepster(‘rishikesh’,‘Abc’,‘abc@123’##object 
ob1.display()

Output: 

rishikesh
Abc
Inheritance In Python

Creating Classes and Objects in Python

Advanced OOP Concepts in Python

  • Abstract Classes and Interfaces: Abstract classes cannot be instantiated and serve as blueprints for other classes. Interfaces define a set of methods that must be implemented by classes that implement the interface.
  • Multiple Inheritance: Python supports multiple inheritance, allowing a class to inherit from more than one base class. While powerful, multiple inheritance should be used carefully to avoid confusion and maintainability issues.

Common Mistakes to Avoid in OOP

  • Overcomplicating Designs

It’s easy to overcomplicate designs by creating too many classes or using inheritance excessively. Keeping designs simple and focused on solving specific problems is crucial.

  • Violating the Single Responsibility Principle

A class should have a single responsibility. Violating this principle by adding too many unrelated features to a class can make the codebase harder to manage.

Conclusion

Object-Oriented Programming is a powerful paradigm that enhances code organization, reusability, and maintainability.Python’s support for OOP concepts like classes, objects, inheritance, and polymorphism makes it an ideal language for practicing OOP principles. By incorporating OOP into your coding practices, you can write cleaner, more efficient, and more robust code that can tackle a wide range of software development challenges.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription