Python oops concepts with examples: Step-by-step guide

Python oops concepts with examples: Step-by-step guide

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data and code that manipulates that data. OOPs concept are designed to eliminate the need for a lot of the code that is required in procedural programming and to make it easier to reuse code across projects.

Introduction

Classes and Objects are the main building blocks of object-oriented programming. It can be regarded as an analogy to structures in C programming. Imagine you own a car showroom who have cars with different colors and engines. each type of car comes with specific colors which would consume a lot of data to store in a database. hence creating objects and classes to be simple, In a class called a car, there can be objects created called a blue car and black car, and so on. Now that we have understood the use of object-oriented let us dive deeper into the concepts of object-oriented programming.

What is Object-Oriented Programming in python?

The concept of using classes and objects in python is called object-oriented programming in python. the concepts of object-oriented programming using python usually consists of inheritance, Data abstraction, encapsulation, and polymorphism. we will talk about all these in the coming sections.

Advantages of OOPs

Here are some of the main benefits of object-oriented programming in python.

  • It provides data abstraction(hiding the details of implementation).
  • It increases code reusability.
  • We can bind data and methods together using encapsulation.
  • Using concepts of oop, we can use inheritance which is not possible using structures.
  • Artificial Intelligence can be possible using oops.
  • All web-based applications use oops.

Difference Between Object-Oriented and Procedural-Oriented Programming

OOP POP
OOP means Object-Oriented Programming.POP means Procedural-Oriented Programming.
C++, Java, and Python are the programming languages that use OOPsC, Pascal, and lisp are the programming languages that use OOPs
Here, the code is divided into Objects.Here, the code is divided into functions.
It follows the Bottom-Top approach.It follows a Top-Down approach.
We can use Classes and Objects.We can use Structures here.
Code can be modified using Access-Modifiers.We can’t use access modifiers here.
Differences Between OOP and POP

What are Classes and Objects in python

Class

A class is the most important of Object-Oriented Programming. We use class keywords to define it. It is stored in memory as a reference.No memory is allocated when a class is created. It can have a Constructor and Destructor of all types. It is stored as a heap in memory.

Object

An Object is a run-time entity. An Object can be defined as an instance of the class. A class is mandatory to create an object. Memory is allocated to a class only when an object is created. All members of a class can be accessed only with the object.

How to Define a Class in Python?

A Class can be defined using the class keyword. The syntax can be seen below.

Syntax

class virat: 
  #StatementsCode language: Python (python)

What is an init Method in Python?

The Init method is a constructor in python. Its syntax is almost the same as defining a function in python. It contains the self keyword. It is defined using two underscores before and after the keyword init.

Self Keyword

A class method has only one difference from an ordinary function. It consists of the self keyword. it is used to initialize variables in the class. it is done using a dot(.) operator.

Syntax

class emp:
      def __init__(self,pay):
          self.pay = pay
e1 = emp(1000000)Code language: Python (python)

Attributes in Python

There are two types of attributes in python. They are the Class attribute and the Instance attribute.

Class Attribute

A class attribute is a Python variable that is owned by a class rather than a particular object. It is shared between all the objects of this class and it is defined outside the initializer method of the class.

Example Code

class person:
     email = "@hotmail.com"
     def __init__(self,name,age):
         self.name = name
         self.age = age
p1 = person("krish",18)
print(p1.name())
print(p1.email())Code language: Python (python)

Output

Krish
@hotmail.comCode language: Python (python)

Here, the email variable can access by all the methods present in class. Hence, the mail is called a Class Attribute.

Instance Attribute

An instance attribute is a Python variable belonging to only one object. This variable is only accessible in the scope of this object and it is defined inside the __init__ method also Instance Attributes are unique to each object.

Example Code

class person:
     def __init__(self,name,age):
         self.name = name
         self.age = age
p1 = person("krish",18)
print(p1.age)Code language: Python (python)

Output

18Code language: Python (python)

In the above code, attributes name and age are instance attributes since they are only accessible in the scope of the object p1, and also the values are unique to each object.

Creating an Object in Class

An Object to a class can be created using the syntax mentioned below.

Syntax

class python:
   #statements
myObj = python()Code language: Python (python)

Fundamentals of Object-Oriented Programming

Let us discuss the four pillars of OOPs concept.

Encapsulation

Wrapping up data and member functions into a single unit is called Encapsulation. For example, when you try to access a student, the data bound to the student which is his roll number, academic records, and which year he is studying, all can be accessed using the concept of Encapsulation.

Polymorphism

Poly means many and morph means forms, which sums up to many forms. It talks about the relation between the method with the same name in the base class and the inherited class and which should be accessed at which time.

Data Abstraction

Data Abstraction is the process of hiding the details of implementation. Which increases the security of data.

Inheritance

The process of inheriting the methods defined in the base class in the child class is called inheritance. There are five types of Inheritance.

Single Inheritance

In this type of inheritance, Only one child is present which is inherited from the base class. the child class can access all methods defined in the base class. But the base class can’t access methods defined in the child class.

Multiple Inheritance

In this type, two base classes are present which contribute to forming a single child class hence, the child class has access to both base classes.

Multi-Level Inheritance

In this type of Inheritance, a child class is inherited from a base class and another child class is inherited from the inherited class.

Hierarchical Inheritance

This is the type of inheritance where two different child classes are inherited from the same base class. Methods defined in one child class can’t be accessed from others.

Hybrid Inheritance

Hybrid inheritance is the combination of all the above-listed inheritances. It is a little bit complicated compared to others. Since It is a combination of all.

Abstract Class

Abstract Classes are the type of classes where you cannot create an object to the class. We can access methods of the abstract class using concepts of Inheritance. An abstract class is defined using the keyword abstract method below the class definition. It helps in data abstraction.

Syntax

class Myex:
      @abstractmethodCode language: Python (python)

In the below example, you can see to access methods of abstract classes.

Example Code

class Example:
      @abstractmethod
      def nothing(self):
          print("There is Nothing Here")
class ex2(Example):
      def show(self):
          print("There is nothing to show")
myObj = ex2()
ex2.show()
ex2.nothing()Code language: Python (python)

Output

There is nothing to show
There is Nothing HereCode language: Python (python)

FAQs

1. What are the OOP concepts in Python?

Inheritance, Encapsulation, Polymorphism, Data Abstraction, Objects, and Classes are the main concepts of OOP in Python.

2. What are the 4 basics of OOP?

Abstraction, encapsulation, inheritance, and polymorphism are four of the main principles of object-oriented programming.

3. Is OOP in Python difficult?

No, It is actually a bit easy compared to others due to its simpler syntax.

4. How many OOPs concepts are there in Python?

There are four main concepts in python. They are Abstraction, encapsulation, inheritance, and polymorphism.

5. What are the 4 pillars of OOPs concept in Python?

Abstraction, encapsulation, inheritance, and polymorphism are often considered the 4 main pillars of OOPs in Python.

Conclusion

All the topics discussed above are the main building blocks of Object-Oriented Programming. These OOPs concept are somewhat tough but important. Hence, Practice more and master them. You will understand the importance of OOPs when you step into the Implementation Of Data Structures. So, Practice More.

Happy Coding!

Sharing is caring

Did you like what Krishna Bethina wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far