DEV Community

Moyeen Haider
Moyeen Haider

Posted on

Inheritance in Dart

What is Inheritance?

Inheritance is like passing down traits from parents to children. It allows a new class (child) to inherit the characteristics and behaviors of an existing class (parent).

Why is it Important?

Inheritance promotes code reuse, making it easier to create and maintain code. It establishes a relationship between classes, where the child class can use the properties and methods of the parent class.

Imagine: Think of a superhero (child class) inheriting superpowers (methods and properties) from their superhero parent. Each new superhero doesn’t need to learn how to fly; they inherit the ability from their superhero parent!

Let’s try to Visualize the below code:

/**
 * Inheritance Example
 * 
 * Inheritance is like passing down traits from parents to children.
 * It allows a new class (child) to inherit the characteristics and behaviors of an existing class (parent).
 * This promotes code reuse, making it easier to create and maintain code.
 * It establishes a relationship between classes, where the child class can use the properties and methods of the parent class.
 * 
 * Why is Inheritance Important?
 * Inheritance is important because it promotes code reuse and establishes a hierarchy between classes.
 * It allows the child class to leverage the properties and methods of the parent class, reducing redundancy and making code more maintainable.
 * 
 * Imagine: Think of inheritance as a superhero (child class) inheriting superpowers (methods and properties) from their superhero parent.
 * Each new superhero doesn't need to learn how to fly; they inherit the ability from their superhero parent!
 */

// Define a class representing a Superhero (parent class)
class Superhero {
  // Properties (traits) of a superhero
  String name;
  String superpower;

  // Constructor to initialize superhero properties
  Superhero(String name, String superpower) {
    this.name = name;
    this.superpower = superpower;
  }

  // Method to showcase the superhero's abilities
  void useSuperpower() {
    print('$name is using their superpower: $superpower');
  }
}

// Define a class representing a new Superhero (child class) inheriting from the Superhero class
class NewSuperhero extends Superhero {
  // Additional properties specific to the new superhero
  String costumeColor;

  // Constructor to initialize properties of the new superhero and call the parent constructor
  NewSuperhero(String name, String superpower, this.costumeColor) : super(name, superpower);
}

/**
 * Main function where the program execution starts
 * Imagine this as the superhero training ground where new superheroes are being created and showcasing their abilities.
 */
void main() {
  // Create a Superhero object
  Superhero originalHero = Superhero('Superman', 'Flight');

  // Use the superpower of the original superhero
  originalHero.useSuperpower();

  // Create a NewSuperhero object inheriting from the Superhero class
  NewSuperhero newHero = NewSuperhero('Wonder Woman', 'Lasso of Truth', 'Red');

  // Use the superpower of the new superhero
  newHero.useSuperpower();
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)