100% found this document useful (1 vote)
291 views8 pages

OOPs in Python

note

Uploaded by

tudurajanikanta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
291 views8 pages

OOPs in Python

note

Uploaded by

tudurajanikanta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Python Programming

Topperworld.in

OOPs Concepts

• Like other general-purpose programming languages, Python is also an object-


oriented language since its beginning.
• It allows us to develop applications using an Object-Oriented approach.
In Python, we can easily create and use classes and objects.
• An object-oriented paradigm is to design the program using classes and
objects. The object is related to real-word entities such as book, house,
pencil, etc.
• The oops concept focuses on writing the reusable code. It is a widespread
technique to solve the problem by creating objects.

❖ Principles of object-oriented programming system


• Class
• Object
• Method
• Inheritance
• Polymorphism
• Data Abstraction
• Encapsulation

❖ 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}"

# Creating an instance of the class


my_car = Car("Toyota", "Camry")

# Using the class's methods and attributes


print("Car:", my_car.info())

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}"

# Creating an instance of the Car class


my_car = Car("Toyota", "Corolla")

# Using the object's methods and attributes


print("My car:", my_car.display_info())

Output:

My car: Toyota Corolla

©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

# Creating an instance of the class


circle_instance = Circle(5)

# Calling the method and printing the result


print("Circle area:", circle_instance.area())

Output:

Circle area: 78.5

❖ 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()

animal_sound(cat) # Output: Meow


animal_sound(dog) # Output: Woof
Output:
Meow
Woof

©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

You might also like