boy in green shirt
Photo by CDC on Pexels.com

[Lecture Report] Introduction to System Development, Week 19

— Exploring “Extensible Design” with Inheritance & Polymorphism! —

Building on last week’s lesson on object-oriented basics (classes and instances), this week we tackled inheritance and polymorphism: using existing classes to extend functionality, and changing behavior under the same method name—key ideas for flexible design.


■ Instructor’s Introduction: “How to Model ‘Similar but Slightly Different’?”

Professor Tanaka:

“Dogs and cats are both ‘animals,’ but their sounds differ. That’s where inheritance and polymorphism come in.”

On the board was a diagram:

      Animal
       /  \
     Dog  Cat

Common features go in the parent class; differences go in the child classes.


■ Exercise ①: Consolidate Common Behavior with Inheritance

We first created a parent Animal class, then had Dog and Cat inherit from it:

class Animal:
    def __init__(self, name):
        self.name = name
    
    def speak(self):
        print(self.name + " is about to say something...")

class Dog(Animal):
    def speak(self):
        print(self.name + ": Woof woof!")

class Cat(Animal):
    def speak(self):
        print(self.name + ": Meow!")
  • Student A: “I don’t need to rewrite name handling in Dog or Cat!”
  • Professor Tanaka: “Shared behavior lives in the parent; differences live in the children. That’s inheritance.”

■ Exercise ②: Experience Polymorphism

We saw that calling the same speak() method produces different outputs for each class:

animals = [Dog("Pochi"), Cat("Mike"), Animal("???")]

for a in animals:
    a.speak()
  • Student B: “It’s weird but convenient that the same speak() works for all!”
  • Professor Tanaka: “Being able to call the same method name keeps your code simple.”

■ Independent Practice: Design with Inheritance

In the latter half, students implemented inheritance & polymorphism on a theme of their choice.

💡 Sample Themes

  • GameCharacter → Warrior / Wizard / Healer (different attack methods)

  • Question → YesNoQuestion / MultipleChoiceQuestion (different display and grading)

  • Vehicle → Car / Bicycle / Train (different movement and speed calculations)

  • Student C: “I made a Question class and overrode it for different question types!”

  • Student D: “I overrode the attack method to vary the animation per character!”


■ Instructor’s Note

“Inheritance and polymorphism are keys to reuse and extensibility. But avoid needless inheritance—always ask, ‘Is this really common behavior?’”


■ Next Week’s Preview: Starting a Mini OOP Project!

From next week onward, we’ll tackle a small OOP project:

  1. Draw class diagrams (UML style)
  2. Apply inheritance and polymorphism
  3. Write test code to verify behavior

Let’s deepen our “design and build” skills!


Week 19 illuminated how to represent “similar but different” in code. Our first-year students, now captivated by class design, are eager for the next project.

By greeden

Leave a Reply

Your email address will not be published. Required fields are marked *

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)