On this page11 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. The Idea
  4. 2. extends and super
  5. 3. "Is-a" and the is Keyword
  6. 4. One Parent Only
  7. Code Examples
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 042: Inheritance

DayExtra Topics
Lecture042 of 057
Estimated video length12 min
TargetDart 3.12
LevelBeginner

A dog is an animal. A manager is an employee. Inheritance lets one class build on another, so the shared parts are written once and every child class gets them for free.

Contents

Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways


Prerequisites

Lectures 030–033: classes, objects, constructors and this.


Learning Objectives

  • Explain what inheritance is and why it saves code
  • Create a child class with extends
  • Pass values to the parent constructor with super
  • Add new fields and methods to a child class
  • Check an object's type with is

1. The Idea

Without inheritance, a Dog class and a Cat class would both repeat the same name field and the same eat() method. With inheritance, the shared parts live in one parent class (Animal), and each child class adds only what is special about it.

WordMeaning
parent class / superclassthe class being built on (Animal)
child class / subclassthe class that builds on it (Dog)
extendsthe keyword that makes a class a child

2. extends and super

dart
class Animal {
  String name;
  Animal(this.name);

  void eat() {
    print('$name is eating.');
  }
}

class Dog extends Animal {
  Dog(super.name);          // hand `name` to Animal's constructor

  void bark() {
    print('$name says Woof!');
  }
}

class Cat extends Animal {
  bool indoor;
  Cat(super.name, this.indoor);   // one value for Animal, one for Cat

  void meow() {
    print('$name says Meow! (indoor: $indoor)');
  }
}

super.name in the constructor means "pass this value to the parent's constructor". The child gets everything the parent has (name, eat()) and adds its own (bark(), indoor, meow()).

dart
void main() {
  var dog = Dog('Tommy');
  dog.eat();     // inherited from Animal
  dog.bark();    // Dog's own

  var cat = Cat('Kitty', true);
  cat.eat();
  cat.meow();
}

Output

code
Tommy is eating.
Tommy says Woof!
Kitty is eating.
Kitty says Meow! (indoor: true)

3. "Is-a" and the is Keyword

A Dog is an Animal, so is confirms both. A Cat is not a Dog.

dart
print(dog is Animal);   // true
print(dog is Dog);      // true
print(cat is Dog);      // false

Use inheritance only when the "is-a" sentence makes sense. A car is a vehicle: yes. A car is an engine: no. A car has an engine, so the engine should be a field.

4. One Parent Only

A Dart class can extend one class. But chains are fine: Puppy extends Dog, and Dog extends Animal, so a Puppy has everything from both.


Code Examples

Example 1: Staff at a Company

dart
class Employee {
  final String name;
  final double salary;
  Employee(this.name, this.salary);

  void showInfo() {
    print('$name earns Rs $salary');
  }
}

class Manager extends Employee {
  final int teamSize;
  Manager(super.name, super.salary, this.teamSize);

  void holdMeeting() {
    print('$name holds a meeting with $teamSize people.');
  }
}

class Developer extends Employee {
  final String language;
  Developer(super.name, super.salary, this.language);

  void writeCode() {
    print('$name writes $language code.');
  }
}

void main() {
  var m = Manager('Sana', 250000, 6);
  var d = Developer('Bilal', 180000, 'Dart');

  m.showInfo();
  m.holdMeeting();
  d.showInfo();
  d.writeCode();
}

Output

code
Sana earns Rs 250000.0
Sana holds a meeting with 6 people.
Bilal earns Rs 180000.0
Bilal writes Dart code.

showInfo() was written once in Employee, and both Manager and Developer use it.


Common Beginner Mistakes

1. Forgetting to pass values to the parent

dart
class Dog extends Animal {
  Dog(String name);
}
code
Error: The implicitly called unnamed constructor from 'Animal' has required parameters.
Try adding an explicit super initializer with the required arguments.

Animal needs a name. Write Dog(super.name);

2. Calling a child's method on a parent object

dart
var a = Animal();
a.bark();
code
Error: The method 'bark' isn't defined for the type 'Animal'.

Every dog is an animal, but not every animal can bark.

3. Extending two classes

dart
class C extends A, B {}
code
Error: Each class definition can have at most one extends clause.

4. Using inheritance for "has-a": class Car extends Engine is wrong. Give Car an Engine engine; field instead.


Practice Questions

  1. In class Laptop extends Computer, which is the parent and which is the child?
  2. What does super.name do in a constructor?
  3. Can a class extend two classes in Dart?
  4. class Truck extends Vehicle. Is Truck('Hino') is Vehicle true or false?
  5. Should Student extends School be used? Why or why not?

Practice Answers

  1. Computer is the parent; Laptop is the child.
  2. It passes that value to the parent class's constructor.
  3. No, only one. Chains (A extends B, B extends C) are allowed.
  4. True. A truck is a vehicle.
  5. No. A student is not a school; a school has students. Use a list field instead.

Key Takeaways

  • Inheritance puts shared code in a parent class; children reuse it.
  • class Child extends Parent creates the relationship.
  • super.field in a constructor passes values to the parent.
  • Children can add their own fields and methods.
  • Only one extends per class; use it only for true "is-a" relationships.