Python OOPs Concepts

Like other programming languages, Python is also an object-oriented language.  It allows us to built applications using an Object-Oriented approach.

Principles of object-oriented programming system are given below:

Object:

A unique instance of a data structure that’s defined by its class that has state and behaviour. An object comprises both data members and methods.

Class:

A user-defined entity for an object that determines how an object will behave and what the object will contain. It is a set of attributes to build an object. The attributes class variables, instance variables and methods accessed via dot notation.

Example.

#Create a class 
class animal:
    name = 'cat'
#Create an object of class animal
obj1 = animal()  
print(obj1.name)   # Output: cat

The __init__() Function :

It is a built-in function in Python Class which is always executed when the Class is being initiated.

Use the __init__() function to assign values to object properties or other operations that are necessary to execute when the object is being constructed.

class animal:
    def __init__(self,name,age):
        self.name = name
        self.age = age
#Create class object.
obj1 = animal("dog",25)
print("name of animal :",obj1.name)   #Outout: name of animal : dog
print("age of animal:",obj1.age)      #Output: age of animal: 25

The self Parameter :

The self parameter is a reference to the current instance of the class. we can access the variables and methods of the class by using the “self” keyword in python. It has to be the first parameter of any function in the class.

Methods: 

A method is a function that is defined in a class definition and associated with an object.

Example.

class animal:
    def __init__(self,name,age):
        self.name = name
        self.age = age    
    def print_fn(self):
        print("Hello, I am a " + self.name)
obj1 = animal("dog",25)
obj1.print_fn()
#Output: Hello, I am a dog

Inheritance: 

Inheritance is the most significant concept in object-oriented programming. It is a parent-child phenomenon.  The child object can obtain all the properties and behaviours of the parent object. A class is called the derived class or child class which derived the properties and behaviour of another class. This another class whose properties are acquired is called the parent class or base class.

Example.

#Parent Class
class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname
    def printname(self):
        print(self.firstname, self.lastname)

let’s create a child class, which derived the functionality from a parent class. Set the parent class name as a parameter when creating child class.

#Child class
class Student(Person):
    student_id = 25

Now, Student class can access all properties and methods of Person class.

x = Student("Johnathan", "Doq")
x.printname()    #Output: Johnathan Doq

The child class inherits all properties and method of parent class except __init__() function.  The __init__() function of child class overrides the inheritance of the parent’s __init__() function.

To inherit the parent’s __init__() function, add a call to the parent’s __init__() function using super() function.

#Parent Class
class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname
    def printname(self):
        print(self.firstname, self.lastname)
        
#child class
class Student(Person):
    def __init__(self, fname, lname,year):
        super().__init__(fname, lname)
        self.year = year
    def welcome(self):
        print("Welcome", self.firstname, self.lastname, "to the class of", self.year)
        
x = Student("mark", "Doq",2019)
x.welcome()
#Output: Welcome mark Doq to the class of 2019

Note:

If a child class and parent class both have same name method, then the child class’s method override the parent class’s method.

Polymorphism:

Object-oriented programming allows you to implement multiple methods with the same name within the same class but has different sets of parameters. The operation performed varies by the object types or arguments of methods.

#Parent Class
class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname
    def printname(self):
        print("In Parent class: ",self.firstname, self.lastname)      
#child class
class Student(Person):
    def __init__(self, fname, lname,year):
        super().__init__(fname, lname)
        self.year = year
    def printname(self):
        print("In Child class :", self.firstname, self.lastname, self.year)
#Create object of child class
x = Student("mark", "Doq",2019)
x.printname()
#Output: In Child class : mark Doq 2019

Leave a Reply

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

Python Tutorials

Python – List Comprehension

Python Set

Python : Assert Keyword

Python Dictionary

Python Tuple

Python List

Python: String

Python: String Formatting using f-string

Python: String Formatting – 2

Python: String Formatting – 1

Python Input/Output

Python Type Conversion and Type Casting

Python Comments

Python – Regular Expression (RegEx)

Python Iterators

Python File Handling

Python Exceptions

Python Function

Python Loops

Python If…Else

Python Collections

Python Operators

Variables in Python

Python Syntax

Introduction to Python

Study Machine Learning