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
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Object-Oriented PHP

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.

Завдання

Swipe to start coding

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.
  • Define a subclass Dog that extends Animal and overrides speak() to output a dog-specific sound.
  • Define a subclass Cat that extends Animal and overrides speak() to output a cat-specific sound.

Рішення

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 6
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you give an example of how to create a class hierarchy for animals?

What is the difference between protected and private visibility in this context?

How does method overriding work when modeling animal behaviors?

close

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.

Завдання

Swipe to start coding

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.
  • Define a subclass Dog that extends Animal and overrides speak() to output a dog-specific sound.
  • Define a subclass Cat that extends Animal and overrides speak() to output a cat-specific sound.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 6
single

single

some-alt