Updated On : May-12,2020 Time Investment : ~15 mins

Python Programming Guide For Beginners - Part 6

As we are learning about Python programming ans its basics, now is the time we understand the OOP concept in Python.

Objected oriented programming as a discipline has gained a universal following among developers. Python, an in-demand programming language also follows an object-oriented programming paradigm. It deals with declaring Python classes and objects which lays the foundation of OOPs concepts. This article on “object oriented programming python” will walk you through declaring python classes, instantiating objects from them along with the four methodologies of OOPs.

Table Of Content

  1. Introduction to Object Oriented Programming in Python
  2. Understanding object oriented programming
  3. What are Classes and Objects?
  4. Object-Oriented Programming methodologies:
  5. Inheritance
  6. Polymorphism
  7. Encapsulation
  8. Abstraction

Introudction To OOP Programing In Python

Object Oriented Programming is a way of computer programming using the idea of “objects” to represents data and methods. It is also, an approach used for creating neat and reusable code instead of a redundant one. the program is divided into self-contained objects or several mini-programs. Every Individual object represents a different part of the application having its own logic and data to communicate within themselves.

Object-Oriented Programming (OOP)

  • It is a bottom-up approach

  • Program is divided into objects

  • Makes use of Access modifiers‘public’, private’, protected’

  • It is more secure

  • Object can move freely within member functions

  • It supports inheritance

What are Classes and Objects?

Definition Of Class: A class is a collection of objects or you can say it is a blueprint of objects defining the common attributes and behavior. Now the question arises, how do you do that?

Well, it logically groups the data in such a way that code reusability becomes easy. I can give you a real-life example- think of an office going ’employee’ as a class and all the attributes related to it like ’emp_name’, ’emp_age’, ’emp_salary’, ’emp_id’ as the objects in Python. Let us see from the coding perspective that how do you instantiate a class and an object.

Class is defined under a “Class” Keyword.

Example:

class class1(): ## class 1 is the name of the class

Objects:

Definition: Objects are an instance of a class. It is an entity that has state and behavior. In a nutshell, it is an instance of a class that can access the data.

Syntax:

obj = class1()

Here obj is the “object “ of class1.

Example - Creating an Object and Class in python:

class employee():
    def __init__(self,name,age,id,salary):
        self.name = name
        self.age = age
        self.salary = salary
        self.id = id

emp1 = employee("Dolly",22,1000,1234)
emp2 = employee("Sunny",23,2000,2234)
print(emp1.__dict__)
{'name': 'Dolly', 'age': 22, 'salary': 1234, 'id': 1000}

Learning About Object-Oriented Programming methodologies:

Object-Oriented Programming methodologies deal with the following concepts.

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

Let us understand the first concept of inheritance in detail.

Inheritance:

Ever heard of this dialogue from relatives “you look exactly like your father/mother” the reason behind this is called ‘inheritance’. From the Programming aspect, It generally means “inheriting or transfer of characteristics from parent to child class without any modification”. The new class is called the derived/child class and the one from which it is derived is called a parent/base class.

Single Inheritance:

Single level inheritance enables a derived class to inherit characteristics from a single parent class.

class employee1(): ##Parent Class
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

class childemployee(employee1):##This is a child class
    def __init__(self, name, age, salary,id):
        self.name = name
        self.age = age
        self.salary = salary
        self.id = id
emp1 = employee1('Dolly',22,1000)

print(emp1.age)
22

Multilevel Inheritance:

Multi-level inheritance enables a derived class to inherit properties from an immediate parent class which in turn inherits properties from his parent class.

Example:

class employee():##Super class
    def __init__(self,name,age,salary):
        self.name = name
        self.age = age
        self.salary = salary
class childemployee1(employee):##First child class
    def __init__(self,name,age,salary):
        self.name = name
        self.age = age
        self.salary = salary

class childemployee2(childemployee1):##Second child class
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary
emp1 = employee('Dolly',22,1000)
emp2 = childemployee1('Sunny',23,2000)

print(emp1.age)
print(emp2.age)
22
23

Hierarchical Inheritance:

Hierarchical level inheritance enables more than one derived class to inherit properties from a parent class.

Example:

class employee():
    def __init__(self, name, age, salary):     ##Hierarchical Inheritance
        self.name = name
        self.age = age
        self.salary = salary

class childemployee1(employee):
    def __init__(self,name,age,salary):
        self.name = name
        self.age = age
        self.salary = salary

class childemployee2(employee):
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary
emp1 = employee('Dolly',22,1000)
emp2 = employee('Sunny',23,2000)

print(emp1.age)
print(emp2.age)
22
23

Multiple Inheritance:

Multiple level inheritance enables one derived class to inherit properties from more than one base class.

Example:

class employee1():##Parent class
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

class employee2():##Parent class
    def __init__(self,name,age,salary,id):
         self.name = name
         self.age = age
         self.salary = salary
         self.id = id

class childemployee(employee1,employee2):
    def __init__(self, name, age, salary,id):
        self.name = name
        self.age = age
        self.salary = salary
        self.id = id
emp1 = employee1('Dolly',22,1000)
emp2 = employee2('Sunny',23,2000,1234)

print(emp1.age)
print(emp2.id)
22
1234

Polymorphism:

You all must have used GPS for navigating the route, Isn’t it amazing how many different routes you come across for the same destination depending on the traffic, from a programming point of view this is called ‘polymorphism’. It is one such OOP methodology where one task can be performed in several different ways. To put it in simple words, it is a property of an object which allows it to take multiple forms.

Polymorphism is of two types:

  • Compile-time Polymorphism
  • Run-time Polymorphism
  • Compile-time Polymorphism:
  • A compile-time polymorphism also called as static polymorphism which gets resolved during the compilation time of the program. One common example is “method overloading”. Let me show you a quick example of the same.
class employee1():
    def name(self):
        print("Dolly Is her name")

    def salary(self):
        print("3000 is her salary")

    def age(self):
        print("22 is her age")

class employee2():
    def name(self):
        print("Sunny is his name")

    def salary(self):
        print("4000 is his salary")

    def age(self):
        print("23 is his age")

def func1(obj):##Method Overloading

        obj.name()
        obj.salary()
        obj.age()

obj_emp1 = employee1()
obj_emp2 = employee2()

func1(obj_emp1)
func1(obj_emp2)
Dolly Is her name
3000 is her salary
22 is her age
Sunny is his name
4000 is his salary
23 is his age

Run-time Polymorphism:

A run-time Polymorphism is also, called as dynamic polymorphism where it gets resolved into the run time. One common example of Run-time polymorphism is “method overriding”. Let me show you through an example for a better understanding.

Example:

class employee():
   def __init__(self,name,age,id,salary):
       self.name = name
       self.age = age
       self.salary = salary
       self.id = id
def earn(self):
        pass

class childemployee1(employee):

   def earn(self):##Run-time polymorphism
      print("no money")

class childemployee2(employee):

   def earn(self):
       print("has money")

c = childemployee1
c.earn(employee)
d = childemployee2
d.earn(employee)
no money
has money

Encapsulation:

In a raw form, encapsulation basically means binding up of data in a single class. Python does not have any private keyword, unlike Java. A class shouldn’t be directly accessed but be prefixed in an underscore.

Let me show you an example for a better understanding.

Example:

class employee(object):
    def __init__(self):
        self.name = 'Dolly'
        self.age = 22
        self.salary = 1234

object1 = employee()
print(object1.name)
print(object1.age)
print(object1.salary)
Dolly
22
1234

Abstraction:

Suppose you booked a movie ticket from bookmyshow using net banking or any other process. You don’t know the procedure of how the pin is generated or how the verification is done. This is called ‘abstraction’ from the programming aspect, it basically means you only show the implementation details of a particular process and hide the details from the user. It is used to simplify complex problems by modeling classes appropriate to the problem.

An abstract class cannot be instantiated which simply means you cannot create objects for this type of class. It can only be used for inheriting the functionalities.

Example:

from abc import ABC,abstractmethod
class employee(ABC):
    def emp_id(self,id,name,age,salary):    ##Abstraction
        pass

class childemployee1(employee):
    def emp_id(self,id):
        print("emp_id is 12345")

emp1 = childemployee1()
emp1.emp_id(id)
emp_id is 12345
Dolly Solanki  Dolly Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.


Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription