Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Assignment 9: OOPS Task - Utkarsh Gaikwad

Assignment pdf link

Question 1

Question 1: Create a vehicle class with an init method having instance


variables as name_of_vehicle, max_speed and average_of_vehicle.

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')

My vehicle name is Kia Seltos


It has max speed of 170 kmph
It has Average Mileage of 18.5 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

Question 3: Q3. What is multiple inheritance? Write a python code to


demonstrate multiple inheritance.

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!")

# creating a class for mother


class Mom():
# method for parent class 2
def cooking(self):
print("Mom cooks well")

# creating derived class which inherits from 2 parent classes


class Child(Dad, Mom):
def sing_and_foodie(self):
print("Kid loves to sing and is a foodie")

In [6]:
# creating object of the new derived class
child = Child()

# calling methods of parent classes and derived class


child.guitar()
child.cooking()
child.sing_and_foodie()

Dad plays guitar!


Mom cooks well
Kid loves to sing and is a foodie

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]:

# Example setter function


emp1.set_employee_id('007')

In [10]:
# Checking new employee id
emp1.get_employee_id()
Out[10]:
'007'

Question 5

Question 5 : What is method overriding in python? Write a python code to


demonstrate method overriding

Answer : Method overriding is a feature of object-oriented programming


languages where the subclass or child class can provide the program
with specific characteristics or a specific implementation process of
data provided that are already defined in the parent class or
superclass.
In [11]:
# Showing above example
class Animal:
def Walk(self):
print('Hello, I am the parent class Animal')

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

#Invoking Parent class through object r


r = Animal()
r.Walk()

#Invoking Child class through object r


r = Cat()
r.Walk()

Hello, I am the parent class Animal


Hello, I am the child class Cat

You might also like