Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Challenge: Model a Simple Inheritance Hierarchy | OOP Principles and Advanced Features
Object-Oriented PHP
セクション 2.  6
single

single

bookChallenge: Model a Simple Inheritance Hierarchy

メニューを表示するにはスワイプしてください

Recap: Inheritance, Visibility, and Overriding

You have learned that inheritance allows you to create a new class based on an existing one, using the extends keyword. This lets you reuse code and build logical relationships between classes. Visibility keywords—public, private, and protected—control which properties and methods are accessible from outside or inside the class and its subclasses. Method overriding lets a subclass provide its own implementation of a method defined in its parent class.

You are now ready to apply these concepts by modeling a simple class hierarchy representing animals and their behaviors.

タスク

スワイプしてコーディングを開始

Create a class hierarchy to model animals and their sounds.

  • Define a base class Animal with a public method speak() that outputs a generic message.
    • The speak() method should output a message like: "Some generic animal sound".
  • Define a subclass Dog that extends Animal and overrides speak() to output a dog-specific sound.
    • The speak() method should output: "Woof!".
  • Define a subclass Cat that extends Animal and overrides speak() to output a cat-specific sound.
    • The speak() method should output: "Meow!"

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  6
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt