Professional Documents
Culture Documents
Assignment 9 Utkarsh
Assignment 9 Utkarsh
Question 1
Answer :
In [1]:
class vehicle :
def __init__(self,name_of_vehicle,max_speed,average_of_vehicle):
"""
This is init constructor definition which intializes all input variables
for the class vehicle
"""
self.name_of_vehicle = name_of_vehicle
self.max_speed = max_speed
self.average_of_vehicle = average_of_vehicle
In [2]:
# Example
my_car = vehicle('Kia Seltos',170,18.5)
print(f'My vehicle name is {my_car.name_of_vehicle}')
print(f'It has max speed of {my_car.max_speed} kmph')
print(f'It has Average Mileage of {my_car.average_of_vehicle} kmpl')
Question 2
Question 2: Create a child class car from the vehicle class created in Que
1, which will inherit the vehicle class.Create a method named
seating_capacity which takes capacity as an argument and returns the
name of the vehicle and its seating capacity.
Answer :
In [3]:
class car(vehicle):
def seating_capacity(self,capacity):
"""
This function takes capacity as argument
and returns vehicle name and seating capacity
"""
self.capacity = capacity
return self.name_of_vehicle, self.capacity
In [4]:
# Example
friends_car = car('MG Hector',195,14)
friends_car.seating_capacity(5)
Out[4]:
('MG Hector', 5)
Question 3
Answer : if a child class inherits from more than one class, i.e. this child
class is derived from multiple classes, we call it multiple inheritance in
Python.
This newly derived child class will inherit the properties of all the classes
that it is derived from.
In [5]:
# creating class for father
class Dad():
# writing a method for parent class 1
def guitar(self):
print("Dad plays guitar!")
In [6]:
# creating object of the new derived class
child = Child()
Question 4
Question 4 : What are getter and setter in python? Create a class and
create a getter and a setter method in this class.
Answer :
1. Getters : These are the methods used in Object-Oriented Programming (OOPS) which helps to access the
private attributes from a class.
2. Setters : These are the methods used in OOPS feature which helps to set the value to private attributes in a
class.
In [7]:
# Creating a class employee with getter and setter functions for employee_id variable
class employee:
def __init__(self,name,employee_id):
"""
This constructor function creates 2 private vairables
name and employee_id which cannot be directly accessed
"""
self.__name = name
self.__employee_id = employee_id
def get_employee_id(self):
"""
This is getter function gets the employee id of the given employee
"""
return self.__employee_id
def set_employee_id(self,new_id):
"""
This is a setter function and sets the employee id for given employess
"""
self.__employee_id = new_id
In [8]:
# Example with getter function
emp1 = employee('Utkarsh','123')
emp1.get_employee_id()
Out[8]:
'123'
In [9]:
In [10]:
# Checking new employee id
emp1.get_employee_id()
Out[10]:
'007'
Question 5
def communicate(self):
print('This is how animal communicates.')
class Cat(Animal):
def Walk(self):
print('Hello, I am the child class Cat')
def meow(self):
print('Cat communicates with Meows')
In [12]:
# Here method Walk is overridden