Computer Science: An Overview: Global Edition (12th Edition)

Published by Pearson Higher Education
ISBN 10: 1292061162
ISBN 13: 978-1-29206-116-0

Chapter 6 - Programming Languages - Chapter Review Problems - Page 327: 49

Answer

See explanation

Work Step by Step

Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit properties and behaviors from another class, known as the superclass or base class. Let's consider an example in Python where we want to create classes for different types of sports, such as "Soccer," "Basketball," and "Tennis." Create a base class called "Sport": class Sport: def __init__(self, name, players, rules): self.name = name self.players = players self.rules = rules def play(self): print(f"Playing {self.name} with {self.players} players.") Now, we can create subclasses for each specific type of sport, inheriting from the "Sport" base class and adding sport-specific attributes and methods. class Soccer(Sport): def __init__(self, players, rules, field_size): super().__init__("Soccer", players, rules) self.field_size = field_size def play(self): super().play() print(f"Field size: {self.field_size}") For Basketball: class Basketball(Sport): def __init__(self, players, rules, court_size): super().__init__("Basketball", players, rules) For Tennis: class Tennis(Sport): def __init__(self, players, rules, court_type): super().__init__("Tennis", players, rules) self.court_type = court_type def play(self): super().play() print(f"Court type: {self.court_type}") self.court_size = court_size def play(self): super().play() print(f"Court size: {self.court_size}")
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.