56 - Intro to oops

Introduction to Object-oriented programming

PP & OOP.png

In programming languages, mainly there are two approaches that are used to write program or code.

  1. Procedural Programming
  2. Object-Oriented Programming

The procedure we are following till now is the “Procedural Programming” approach.


So, in this session, we will learn about Object Oriented Programming (OOP).

OOP.png

The basic idea of object-oriented programming (OOP) in Python is to use classes and objects to represent real-world concepts and entities.

Object:

An object is an instance of a class, and it contains its own data and methods.
For example, you could create a class called "Person" that has properties such as name and age, and methods such as speak() and walk().
Each instance of the Person class would be a unique object with its own name and age, but they would all have the same methods to speak and walk.

Class:

A class is a blueprint or template for creating objects.
It defines the properties and methods that an object of that class will have.

Properties:

Properties are the data or state of an object.

Methods:

methods are the actions or behaviour that an object can perform.

Encapsulation:

One of the key features of OOP in Python is encapsulation,
which means that the internal state of an object is hidden and can only be accessed or modified through the object's methods.

  • This helps to protect the object's data and prevent it from being modified in unexpected ways.

Inheritance:

Another key feature of OOP in Python is inheritance,
which allows new classes to be created that inherit the properties and methods of an existing class.

  • This allows for code reuse and makes it easy to create new classes that have similar functionality to existing classes.

Polymorphism:

Polymorphism is also supported in Python,
which means that objects of different classes can be treated as if they were objects of a common class.

  • This allows for greater flexibility in code and makes it easier to write code that can work with multiple types of objects.
In summary

OOP in Python allows developers to model real-world concepts and entities using classes and objects, encapsulate data, reuse code through inheritance, and write more flexible code through polymorphism.