Ad space (banner)
Lesson 13 / 20

Inheritance

This lesson covers inheritance, which builds a new class on top of an existing one.

You inherit by writing class Child extends Parent { ... }. Adding @override lets you replace a method of the parent class.

The sample code inherits class Animal with class Dog extends Animal and overrides speak() with dog-specific behaviour.

A common early stumble is passing the constructor along. A child class constructor has to call super(name) to run the parent's initialisation.

In professional work, it is common in Flutter to build a custom widget with a shared style by inheriting from an existing one.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Dart directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)