Editor

Create an Animal ABC

Intermediate · Write the code · from Python Abstract Base Classes: Enforce Interfaces with ABC

From the lesson: Python Abstract Base Classes: Enforce Interfaces with ABC

Imagine you're an architect handing blueprints to a construction crew. The blueprint says "there must be a front door here" and "the roof goes here." The crew can choose the materials and style, but they must include those parts. If they skip the front door, the building inspector will reject the project.

Read the lesson

Create an abstract class called Animal that inherits from ABC. It should have two abstract methods: speak() and move(). Then create a Dog class that inherits from Animal where speak() returns the string 'Woof!' and move() returns 'Runs on four legs'. Finally, create a dog and print both methods.

from abc import ABC, abstractmethod
# Create the Animal ABC and Dog class

More Abstract Base Class exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz