Unit 5 - Lesson 5 | Codetantra Python

Unit 5 - Lesson 5 | Codetantra Python

Unit 5 - Lesson 5

54.1.1. OOPS Concepts - Inheritance and Polymorphism

(a) Polymorphism can be defined as ability where an entity can behave differently depending on the context.
(b) The purpose of inheritance is to avoid redundancy.
(c) Modularity reduces the complexity of the program.

54.1.2. Understanding Inheritance

(a) Inheritance is implemented by creating a derived class that can have the features of the base class in addition to having features of it own.
(b) In Python , a base class is created like a regular class.
(c) A derived class is created by passing the base class name as a parameter to the derived class.

54.1.3. Writing a simple base class and derived class

class Car:
    def setenginemodel(self,engine):
        self.engine = engine
    def getenginemodel(self):
        print(self.engine)
# Write your code here..

class Honda(Car):
    def setcarmodel(self,model):
        self.model = model
    def getcarmodel(self):
        print(self.model)

mycar = Honda()
mycar.setenginemodel('EK-1')
mycar.setcarmodel('V6')
print('Car Details:')
mycar.getenginemodel()
mycar.getcarmodel()

54.1.4. Using base and derived class

class Person:
    def setname(self,name):
        self.name = name
    def getname(self):
        print(self.name)
    #Fill in the missing code

class Student(Person):
    def setage(self,age):
        self.age = age
    def getage(self):
        print(self.age)

name = input("Please enter a name: ")
age = int(input("Please enter age: "))

s1 = Student()

s1.setname(name)
s1.setage(age)

s1.getname()
s1.getage()

54.1.5. Understanding Multiple Inheritance

class Car:
    def setenginemodel(self, engine):
        self.engine = engine
    def getenginemodel(self):
        print(self.engine)
class Tyre:
    def settyrenumber(self, num):
        self.num = num
    def gettyrenumber(self):
        print(self.num)
class Honda(Car,Tyre):
    def setcarmodel(self, model):
        self.model = model
    def getcarmodel(self):
        print(self.model)

accord = Honda()
accord.setenginemodel('EK-1')
accord.setcarmodel('V6')
accord.settyrenumber(236)
print('Car Details: ')
accord.getenginemodel()
accord.getcarmodel()
accord.gettyrenumber()

54.1.6. OOPS Concepts

(c) Ability of an entity to behave differently in different contexts.

54.1.7. Multilevel inheritance

class Person:
#fill in the missing code
    def setname(self,name):
        self.name = name
    def getname(self):
        print(self.name)

class Student(Person):
    def setage(self,age):
        self.age = age
    def getage(self):
        print(self.age)

class Address(Student):
    def setaddress(self,address):
        self.address = address
    def getaddress(self):
        print(self.address)

s1 = Address()
Name = input()
Age = int(input())
Place = input()

s1.setname(Name)
s1.setage(Age)
s1.setaddress(Place)

s1.getname()
s1.getage()
s1.getaddress()

54.1.8. Write a program for Multiple Inheritance.

class vehicle:
    '''General Vehicle class'''
    def __init__(self,name,price,regno):
        self.name= name
        self.price= price
        self.regno = regno

# write your code here

class car(vehicle):
    '''Class car inherits from Vehicle'''
    def __init__(self, name, price, regno,gear):
        super().__init__(name,price,regno)
        self.gear = gear

# write your code here

class boat(vehicle):
    '''Class boat inherits from vehicle'''  
    def __init__(self, name, price, regno):
        super().__init__(name,price,regno)

# write your code here

class hover(car, boat):
    '''Class hovercraft inherits from both car and boat'''
    def __init__(self, name, price, regno,gear):
        super().__init__(name,price,regno,gear)

c1 = car('toyota', 1500000, 'car2121', 'auto')
b1 = boat('maruti', 1000000, 'boat0121')
h1 = hover('toyota', 1500000, 'hover1212', 'manual')
print(type(c1).__name__, "  ", c1.name, "   ", c1.price, "  ", c1.regno, "  ", c1.gear)
print(type(b1).__name__, "  ", b1.name, "   ", b1.price, "  ", b1.regno)
print(type(h1).__name__, "  ", h1.name, "   ", h1.price, "  ", h1.regno, "  ", h1.gear)
print(c1.__doc__)