OOPs in Python
OOPs in Python
Topperworld.in
OOPs Concepts
❖ Class
• The class can be defined as a collection of objects. It is a logical entity that
has some specific attributes and methods.
• For example: if you have an employee class, then it should contain an
attribute and method, i.e. an email id, name, age, salary, etc.
©Topperworld
Python Programming
Syntax:
class ClassName:
<statement-1>
.
.
<statement-N>
Example:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def info(self):
return f"{self.brand} {self.model}"
Output:
Toyota Camry
©Topperworld
Python Programming
❖ Object
• The object is an entity that has state and behavior. It may be any real-
world object like the mouse, keyboard, chair, table, pen, etc.
• Everything in Python is an object, and almost everything has attributes
and methods. All functions have a built-in attribute __doc__, which
returns the docstring defined in the function source code.
• When we define a class, it needs to create an object to allocate the
memory. Consider the following example.
Example:
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
return f"{self.make} {self.model}"
Output:
©Topperworld
Python Programming
❖ Method
• The method is a function that is associated with an object.
• In Python, a method is not unique to class instances. Any object type can
have methods.
Example:
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
Output:
❖ Inheritance
• Inheritance is the most important aspect of object-oriented programming,
which simulates the real-world concept of inheritance.
©Topperworld
Python Programming
• It specifies that the child object acquires all the properties and behaviors of
the parent object.
• By using inheritance, we can create a class which uses all the properties and
behavior of another class. The new class is known as a derived class or child
class, and the one whose properties are acquired is known as a base class or
parent class.
• It provides the re-usability of the code.
Example:
class Parent:
def method(self):
print("Parent method")
class Child(Parent):
pass
child = Child()
child.method()
Output:
Parent method
❖ Polymorphism
• Polymorphism contains two words "poly" and "morphs". Poly means many,
and morph means shape.
• By polymorphism, we understand that one task can be performed in different
ways.
©Topperworld
Python Programming
• For example - you have a class animal, and all animals speak. But they speak
differently. Here, the "speak" behavior is polymorphic in a sense and
depends on the animal. So, the abstract "animal" concept does not actually
"speak", but specific animals (like dogs and cats) have a concrete
implementation of the action "speak".
Example:
class Cat:
def make_sound(self):
return "Meow"
class Dog:
def make_sound(self):
return "Woof"
def animal_sound(animal):
print(animal.make_sound())
cat = Cat()
dog = Dog()
©Topperworld
Python Programming
❖ Encapsulation
• Encapsulation is also an essential aspect of object-oriented programming. It
is used to restrict access to methods and variables.
• In encapsulation, code and data are wrapped together within a single unit
from being modified by accident.
Example:
class Car:
def __init__(self):
self.__speed = 0 # Encapsulated private attribute
def accelerate(self):
self.__speed += 10
def get_speed(self):
return self.__speed
my_car = Car()
my_car.accelerate()
my_car.accelerate()
print("Current speed:", my_car.get_speed())
Output:
Current speed: 20
©Topperworld
Python Programming
❖ Data Abstraction
• Data abstraction and encapsulation both are often used as synonyms. Both
are nearly synonyms because data abstraction is achieved through
encapsulation.
• Abstraction is used to hide internal details and show only functionalities.
Abstracting something means to give names to things so that the name
captures the core of what a function or a whole program does.
Example:
from abc import ABC, abstractmethod
class AbstractClass(ABC):
@abstractmethod
def method(self):
pass
class ConcreteClass(AbstractClass):
def method(self):
return "Implemented method"
# Uncommenting the following lines will result in an error
# obj = AbstractClass()
# print(obj.method())
obj = ConcreteClass()
print(obj.method())
Output:
Implemented method
©Topperworld