Thursday, January 24, 2019

THE CAR CLASS INHERITS BOTH VEHICLE AND SEDAN CLASS

class Vehicle:

def showVehicleDetails (self):

print("I am vehicle class")

class Sedan:

def showSedanDetails (self):

print("I am sedan class")

#Create Class Car that inherits Vehicle and Sedan

class Car(Vehicle,Sedan):

def showCarDetails (self):

print("I am car class")

#Create object of car Class

car = Car()

#Access Vehicle class method

car.showVehicleDetails()

#Access Sedan class method

car.showSedanDetails()

#Access child class method

car.showCarDetails()

In the script above we have three classes Vehicle, Sedan and Car. The Car class inherits both Vehicle and Sedan class. We then create the object of the Car class and access the Vehicle and Sedan class methods from the car class object. The example shows that a child class that inherits multiple parents has access to all the attributes and methods of all the parent classes. The output of the above example looks like this:

Method Overriding

In addition to having access to parent class methods, a child class can also override parent class method by providing its own definition for the same method name. Take a look at the following example to understand this concept:

#Create Person Class

class Employee:

def printdetails (self):

print("I am an employee of this company")

#Create Class Manager that inherits Employee

class Manager(Employee):

def printdetails (self):