Q:

Class Inheritance using python programming

belongs to collection: Python OOP Exercises

0

Class Inheritance

Given:

Create a Bus class that inherits from the Vehicle class. Give the capacity argument of Bus.seating_capacity() a default value of 50.

Use the following code for your parent Vehicle class.

class Vehicle:
    def __init__(self, name, max_speed, mileage):
        self.name = name
        self.max_speed = max_speed
        self.mileage = mileage

    def seating_capacity(self, capacity):
        return f"The seating capacity of a {self.name} is {capacity} passengers"
 

Expected Output:

The seating capacity of a bus is 50 passengers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • First, use method overriding.
  • Next, use default method argument in the seating_capacity() method definition of a bus class.

Solution:

class Vehicle:
    def __init__(self, name, max_speed, mileage):
        self.name = name
        self.max_speed = max_speed
        self.mileage = mileage

    def seating_capacity(self, capacity):
        return f"The seating capacity of a {self.name} is {capacity} passengers"

class Bus(Vehicle):
    # assign default value to capacity
    def seating_capacity(self, capacity=50):
        return super().seating_capacity(capacity=50)

School_bus = Bus("School Volvo", 180, 12)
print(School_bus.seating_capacity())

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Define a property that must have the same value fo... >>
<< Create a child class Bus that will inherit all of ...