0% found this document useful (0 votes)
8 views

Assignment 6 Utkarsh

The document discusses object-oriented programming concepts like class, object, pillars of OOP, inheritance, and provides examples to explain each concept. It defines a class Triangle to demonstrate class and object, explains the four pillars of OOP - abstraction, encapsulation, inheritance, polymorphism. It also gives examples of different types of inheritance - single, multiple, and multilevel inheritance.

Uploaded by

bubunkumar84
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment 6 Utkarsh

The document discusses object-oriented programming concepts like class, object, pillars of OOP, inheritance, and provides examples to explain each concept. It defines a class Triangle to demonstrate class and object, explains the four pillars of OOP - abstraction, encapsulation, inheritance, polymorphism. It also gives examples of different types of inheritance - single, multiple, and multilevel inheritance.

Uploaded by

bubunkumar84
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment 6: OOPS - Utkarsh Gaikwad

Assignment PDF Link

Question 1

Question 1: Explain Class and Object with respect to Object-Oriented


Programming. Give a suitable example.

Answer : In object-oriented programming, a class is a blueprint for


creating objects (instances of a class). It defines the data and behaviors
(methods) that the objects of the class will have.
1. For example consider a class Triangle , which could have attributes sides a,b,c and methods like perimeter,
area.
2. Each Triangle created will have different sides with unique values of a,b,c . These unique triangles are
objects of class
3. An Object is an instance of class and represents a single entity with its own unique data and behaviours

In [1]:
class Triangle:

"""
Defining Class Triangle Constructor method 3 sides
"""

def __init__(self, a,b,c):


self.a = a
self.b = b
self.c = c

def isTriangle(self):
"""
for triangle to form sum of any two sides
should be greater than third this function checks this
"""
if (self.a + self.b)>self.c and (self.b + self.c)>self.a and (self.a + self.c)>s
elf.b:
x = True
else:
x = False
return x

def perimeter(self):
"""
This function returns perimeter of triangle
p = a+b+c
only if sides a,b,c form a triangle
"""
if self.isTriangle()==True:
p = self.a + self.b + self.c
else:
p = None
print(f'The sides {self.a}, {self.b}, {self.c} do not form a triangle')
return p

def area(self):
"""
This funtion returns area of triangle based on
Herons formula
only if sides a,b,c form a triangle
"""
if self.isTriangle()==True:
s = (self.a+self.b+self.c)/2 #Semi-perimeter calculation
ar = (s*(s-self.a)*(s-self.b)*(s-self.c))**0.5
else:
ar = None
print(f'The sides {self.a}, {self.b}, {self.c} do not form a triangle')
return ar

In [2]:
t1 = Triangle(3,4,5)

In [3]:

print(type(t1))

<class '__main__.Triangle'>

In [4]:
print(f'Sides of object t1 of class Triangle is {t1.a}, {t1.b}, {t1.c}')

Sides of object t1 of class Triangle is 3, 4, 5

In [5]:
t1.isTriangle()
Out[5]:
True

In [6]:
t1.perimeter()

Out[6]:
12

In [7]:
t1.area()
Out[7]:

6.0

In [8]:
t2 = Triangle(1,2,3)
print(t2.isTriangle())

False

In [9]:
t2.perimeter()
The sides 1, 2, 3 do not form a triangle

In [10]:
t2.area()

The sides 1, 2, 3 do not form a triangle

Question 2

Question 2: Name the four pillars of OOPs.

Answer : 4 pillars of Object Oriented Programming are as below :


1. Abstraction: This refers to the idea of hiding the implementation details and only showing the relevant
information to the user. It allows the user to focus on the objects and their behavior, rather than the
underlying code.
2. Encapsulation: This is the mechanism of wrapping data and functions within a single unit (object) to protect
the data from external access and modification. This helps to maintain the integrity of the data and the
object.
3. Inheritance: This is a mechanism of creating new classes (child classes) from existing classes (parent
classes). The child classes inherit the attributes and behaviors of the parent class, and can also have their
own unique attributes and behaviors. This helps to reduce code duplication and promote code reuse.
4. Polymorphism: This refers to the ability of objects of different classes to respond to the same method call in
different ways. This allows objects to be treated as objects of their parent class, while still retaining their
unique behavior. This enables objects to be used interchangeably, reducing code complexity and improving
code maintainability.

These four pillars are essential elements of object-oriented programming and work together to allow for the
creation of robust, flexible, and scalable software systems.

Question 3

Question 3 : Explain why the __init__() function is used. Give a suitable


example.

Answer : The __init__() function is a special method in Python classes,


known as the constructor method.

It is automatically called when an object of the class is created, and is


used to initialize the attributes of the object
1. For Example consider a class 'car' that has attributes 'manufacturer', 'weight' and color
2. __init__() function can be used to initialize attribute manufacturer, weight and color when car object is
created
In [11]:
class car:
"""
Definining a class car with initialized
Manufactured, weight and color attributes
"""

def __init__(self, manufacturer, weight, color):


self.manufacturer = manufacturer
self.weight = weight
self.color = color

In [12]:
# Defining a seltos car object and printing each attribute
seltos = car('Kia',1700,'black')
print(seltos.manufacturer)
print(seltos.weight)
print(seltos.color)

Kia
1700
black

Question 4

Question 4: Why self is used in OOPs?

Answer : In object-oriented programming (OOP), the self keyword is used


as a reference to the instance of the object that is calling the
method.

It is used to access the attributes and methods of the object within the
class
In [13]:
# Consider above example with car class I added a method in this class called details whi
ch uses self
class car:
"""
Definining a class car with initialized
Manufactured, weight and color attributes
"""

def __init__(self, manufacturer, weight, color):


self.manufacturer = manufacturer
self.weight = weight
self.color = color

def details(self):
print(f'This car was manufactured by {self.manufacturer}, its weight is {self.wei
ght} and its color is {self.color} ')

In [14]:
# Explaining Use of above code with car class
# Explaining Use of above code with car class
hector = car('Morris Garages',1600,'Glaze Red')
hector.details()

This car was manufactured by Morris Garages, its weight is 1600 and its color is Glaze Re
d

In above example the 'details()' method uses 'self' to access cars 'manufacturer', 'weight'
and 'color'.

Without 'self', method 'details()' is not able to create any attribute and will give error.

Question 5

Question 5 : What is inheritance? Give an example for each type of


inheritance.

Answer : Inheritance is a mechanism in object-oriented programming


(OOP) that allows a new class to be created based on an existing
class.

The new class inherits the attributes and behaviors of the existing class,
and can also add its own unique attributes and behaviors.
1. Single Inheritance : When child class is derived from only one parent class. This is called single inheritance

Credits for above image - https://www.scaler.com/topics/python/inheritance-in-python/

In [15]:
# Single Inheritance Example
class Brands: #parent_class
brand_name_1 = "Amazon"
brand_name_2 = "Ebay"
brand_name_3 = "OLX"

class Products(Brands): #child_class


prod_1 = "Online Ecommerce Store"
prod_2 = "Online Store"
prod_3 = "Online Buy Sell Store"

obj_1 = Products() #Object_creation


print(obj_1.brand_name_1+" is an "+obj_1.prod_1)
print(obj_1.brand_name_2+" is an "+obj_1.prod_2)
print(obj_1.brand_name_3+" is an "+obj_1.prod_3)

Amazon is an Online Ecommerce Store


Ebay is an Online Store
OLX is an Online Buy Sell Store

1. Multiple Inheritance: When child class is derived or inherited from more than one parent class. This is called
multiple inheritance. In multiple inheritance, we have two parent classes/base classes and one child class
that inherits both parent classes properties.

In [16]:
#example_of_multiple_inheritance

class Brands: #parent_class_1


brand_name_1 = "Amazon"
brand_name_2 = "Ebay"
brand_name_3 = "OLX"

class Products: #parent_class_2


prod_1 = "Online Ecommerce Store"
prod_2 = "Online Store"
prod_3 = "Online Buy Sell Store"

class Popularity(Brands,Products): #child class created from parent class 1 and parent cl
ass 2
prod_1_popularity = 100
prod_2_popularity = 70
prod_3_popularity = 60

obj_1 = Popularity() #Object_creation


print(obj_1.brand_name_1+" is an "+obj_1.prod_1 +" with popularity = "+str(obj_1.prod_1_
popularity))
print(obj_1.brand_name_2+" is an "+obj_1.prod_2 +" with popularity = "+str(obj_1.prod_2_
popularity))
print(obj_1.brand_name_3+" is an "+obj_1.prod_3 +" with popularity = "+str(obj_1.prod_3_
popularity))

Amazon is an Online Ecommerce Store with popularity = 100


Ebay is an Online Store with popularity = 70
OLX is an Online Buy Sell Store with popularity = 60

1. Multilevel Inheritance: In multilevel inheritance, we have one parent class and child class that is derived or
inherited from that parent class. We have a grand-child class that is derived from the child class.
In [17]:
#example_of_multilevel_inheritance

class Brands: #parent_class


brand_name_1 = "Amazon"
brand_name_2 = "Ebay"
brand_name_3 = "OLX"

class Products(Brands): #child_class


prod_1 = "Online Ecommerce Store"
prod_2 = "Online Store"
prod_3 = "Online Buy Sell Store"

class Popularity(Products): #grand_child_class


prod_1_popularity = 100
prod_2_popularity = 70
prod_3_popularity = 60

obj_1 = Popularity() #Object_creation


print(obj_1.brand_name_1+" is an "+obj_1.prod_1+" popularity of "+str(obj_1.prod_1_popula
rity))
print(obj_1.brand_name_2+" is an "+obj_1.prod_2+" popularity of "+str(obj_1.prod_2_popula
rity))
print(obj_1.brand_name_3+" is an "+obj_1.prod_3+" popularity of "+str(obj_1.prod_3_popula
rity))

Amazon is an Online Ecommerce Store popularity of 100


Ebay is an Online Store popularity of 70
OLX is an Online Buy Sell Store popularity of 60

1. Hierarchical inheritance: When we derive or inherit more than one child class from one(same) parent class.
Then this type of inheritance is called hierarchical inheritance

In [18]:
#example of Hierarchical inheritence

class Brands: #parent_class


brand_name_1 = "Amazon"
brand_name_2 = "Ebay"
brand_name_3 = "OLX"

class Products(Brands): #child_class 1


prod_1 = "Online Ecommerce Store"
prod_2 = "Online Store"
prod_3 = "Online Buy Sell Store"

class Popularity(Brands): #child class 2


prod_1_popularity = 100
prod_2_popularity = 70
prod_3_popularity = 60

class Value(Brands): #child class 3


prod_1_value = "Excellent Value"
prod_2_value = "Better Value"
prod_3_value = "Good Value"

obj_1 = Products() #Objects_creation


obj_2 = Popularity()
obj_3 = Value()
print(obj_1.brand_name_1+" is an "+obj_1.prod_1+' with popularity of '+str(obj_2.prod_1_p
opularity)+' and is '+ obj_3.prod_1_value)
print(obj_1.brand_name_2+" is an "+obj_1.prod_2+' with popularity of '+str(obj_2.prod_2_p
opularity)+' and is '+ obj_3.prod_2_value)
print(obj_1.brand_name_3+" is an "+obj_1.prod_3+' with popularity of '+str(obj_2.prod_3_p
opularity)+' and is '+ obj_3.prod_3_value)

Amazon is an Online Ecommerce Store with popularity of 100 and is Excellent Value
Ebay is an Online Store with popularity of 70 and is Better Value
OLX is an Online Buy Sell Store with popularity of 60 and is Good Value

1. Hybrid Inheritance - Inheritance consisting of multiple types of inheritance is called hybrid inheritance.

In [19]:
# hybrid inheritance

class School:
def func1(self):
print("This function is in school.")

class Student1(School):
def func2(self):
print("This function is in student 1. ")

class Student2(School):
def func3(self):
print("This function is in student 2.")

class Student3(Student1, School):


def func4(self):
print("This function is in student 3.")

# Driver's code
object = Student3()
object.func1()
object.func2()
object.func4()

This function is in school.


This function is in student 1.
This function is in student 3.

You might also like