Python Object Oriented Programming Concepts

The OOPs concept simply represents the object-oriented programming concept of any programming language. The oops concept represents the data and methods in terms of the object. Here Python oops concept includes the concept of object, class, method, inheritance, polymorphism, data abstraction, and encapsulation. Python object has two types of characteristics. One is attributes and another one is behavior. The oops concept follows the DRY rule that is Don’t Repeat Yourself.

Class: 

A collection of objects is known as a class in an object-oriented programming language. A class is nothing but a blueprint of an object that describes the common attributes and behavior. 

class student1 (): # create a class

Object: 

Objects are nothing but an instance of a class. Using an object, programmers can access the private data and method of a class. The object is an entity and it also has state and behavior. It represents the real-world objects like the keyboard, mouse, chair, table, pen, etc. In an object-oriented programming language, everything is an object.

s1 = Student1()  # Create an object
print(s1.dept)

Method:

Methods are basically the function that is defined inside the class. Methods are two types. One is built-in methods that are already predefined and another one is user-defined that is defined by the user. Methods are used to define the behavior of an object. These methods are associated with objects.

class student1 (): # creating class
   def __init__( self, name, id ):  # creating a function
             self.name = name # self is an instance of a class
             self.id = id
             self.dept = "student"

   def marks():
        print("Passed")

Inheritance:

 Inheritance is one of the most important parts of the OOPs concept. The process of acquiring all the properties and behavior of the parent class by a child class is known as inheritance. The child class can inherit all the property of the parent class by using inheritance. The class which acquires all the .property and behavior from another class is known as a derived class or child class. The class from which the properties and behaviors are inherited is known as the parent class or base class. One of the biggest advantages of inheritance is the re-usability of code.

class Student1(): # creating a parent class
       def __init__(self, name, id):  # creating a function
                   self.name = name # self is an instance of a class
                   self.id = id
                   self.dept = "student"
       def marks():
             print("Passed")

class Student2(Student1): # Child class & Inheritance is implemented
        def __init__( self, name, id,  age ):   #creating a function
                   self.name = name # self is an instance of a class
                   self.id = id
                   self.dept = "student"
                   self.age = age

Polymorphism:

In this word polymorphism, poly represents many and morph represents the shape. Polymorphism is a way to perform a task in many ways using different procedures. This is another important part of the OOPs concept. Suppose, the fruit is a class and all fruits have their own flavor. So, the flavor is different for different types of fruits. The flavor is the behavior and it depends on the fruits. This concept is nothing but polymorphism.

Data Abstraction:

This is another important concept of object-oriented programming language. Data abstraction is a process to hide private data members and methods from the users and only shows the functionalities. The object of the abstract class cannot be created. This class can be used by inheriting the functionalities. 

Encapsulation:

One of the most important concepts of object-oriented programming languages that are used to wrap data in a single unit. It uses different types of access specifiers to restrict data members and methods from any kind of modification. The private access specifier is not supported by Python. 

Sample Code:

class Student1(): # creating class
       def __init__(self, name, id):  # creating a function
                   self.name = name # self is an instance of a class
                   self.id = id
                   self.dept = "student"
       def marks():
             print("Passed")

class Student2(Student1): # Inheritance is done
        def __init__( self, name, id,  age ):   #creating a function
                   self.name = name # self is an instance of a class
                   self.id = id
                   self.dept = "student"
                   self.age = age

class Student3():
        def marks():
            print("Passed")

def display (obj): # Method Overloading
obj. marks()

s1 = Student2("jack", 1001, 19) # creating objects of student class
print( s1. dept) # Encapsulation is done
print( s1.__dict__ ) # Prints dictionary