Skip to content

Commit a0e8d8e

Browse files
Added Exercise 6
1 parent 71e8b67 commit a0e8d8e

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
""" Class Inheritance """
2+
3+
# Create a Bus child class that inherits from the Vehicle class. The default fare charge for any vehicle is its seating capacity multiplied by 100 (seating capacity * 100).
4+
# If the vehicle is a Bus instance, we need to add an extra 10% to the full fare as a maintenance charge. Therefore, the total fare for a Bus instance will be the final amount, calculated as total fare plus 10% of the total fare. (final amount = total fare + 10% of the total fare.)
5+
# Note: The bus seating capacity is 50, so the final fare amount should be 5500.
6+
7+
# Expected Output :- Total Bus fare is: 5500.0
8+
9+
# Hints:- You need to override the fare() method of the Vehicle class in the Bus class.
10+
# Use super() function within a child class to call methods of a parent (or super) class.
11+
# Method overriding lets a subclass provide its own version of a method that its superclass already has. It allows the subclass to customize or specialize the behavior inherited from the superclass.
12+
13+
class Vehicle:
14+
def __init__(self, name, mileage, capacity):
15+
self.name = name
16+
self.mileage = mileage
17+
self.capacity = capacity
18+
19+
def fare(self):
20+
return self.capacity * 100
21+
22+
class Bus(Vehicle):
23+
def fare(self):
24+
amount = super().fare()
25+
amount += amount * 10 /100
26+
return amount
27+
28+
School_bus = Bus("School Volvo", 12, 50)
29+
print("Total Bus fare is:", School_bus.fare())
30+
31+

0 commit comments

Comments
 (0)