Inheritance in Dart

Inheritance in Dart

What is inheritance?

Let’s forget programming for a moment and think about real life. What do we understand when someones says the word inheritance?

Inheritance is the process of passing on properties from one generation to another. For example, a child inherits the properties of his/her parents and grandparents which continues this from generation to generation.

Likewise, in the world of programming classes pass properties to one another which is known as inheritance.

Familiar with the topic? Play Quiz

Before we dive into inheritance in Dart, let’s first understand the keywords: extends, this and super in Dart.

Keyword Description
extends The keyword extends is used to inherit properties from a parent class.
this The keyword this is used to refer to the current instance of the class.
super The keyword super is used to refer to the parent class.

This keywords are very important to understand and implement inheritance in Dart.

What is Inheritance in Dart Programming?

In programming, inheritance is the process of passing on properties from one class to another.

The class that passes on the properties is called the parent class or the super class. The class that inherits the properties is called the child class or the sub class.

Inheritance is a very important concept in object oriented programming. It allows us to reuse code and also to add more features to a class without modifying it.

Example

Let’s look at an example of inheritance in Dart.

class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

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

  void sleep() {
    print('$name is sleeping');
  }
}

class Dog extends Animal {
  Dog(String name, int age) : super(name, age);

  void bark() {
    print('$name is barking');
  }
}

void main() {
  var dog = Dog('Bruno', 2);
  dog.eat();
  dog.sleep();
  dog.bark();
}

Output

Bruno is eating
Bruno is sleeping
Bruno is barking

code Try Yourself

Types of Inheritance

There are different types of inheritance in Dart. Dart supports the following types of inheritance.

  • Single Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance

Single Inheritance in Dart

Single inheritance is the process of inheriting properties from a single parent class. This means, If there are class A and class B, and class B inherits from class A, then it is called single inheritance.

graph TD
A[class A]-->B[class B]

C[class Animal]-->D[class Human]

E[class Dog]-->F[class GermanShepherd]

Let’s follow the set of steps to achieve the single inheritance.

Step 1: Define a parent class

class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

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

  void sleep() {
    print('$name is sleeping');
  }
}

We have defined a parent class with the name and age as properties and eat() and sleep() as class methods.

Step 2: Delcare the child class and inherit the parent class


class Dog extends Animal {
  Dog(String name, int age) : super(name, age);

  void bark() {
    print('$name is barking');
  }
}

Here, we have defined the Dog class with its own bark() method. Since our parent class in not a zero argument constructor we are making the use of super keyword to pass the parameters to super class constructor.

Step 3: Using the inherited classes.


void main() {
  var dog = Dog('Bruno', 2);
  dog.eat();
  dog.sleep();
  dog.bark();
}

In the above block of code, we have created the object of child class Dog which inherits the Animal class. We have access to sleep() and eat() methods from the parent(Animal) class along with the bark() method of child(Dog) class.

Full Code Example

class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

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

  void sleep() {
    print('$name is sleeping');
  }
}

class Dog extends Animal {
  Dog(String name, int age) : super(name, age);

  void bark() {
    print('$name is barking');
  }
}

void main() {
  var dog = Dog('Bruno', 2);
  dog.eat();
  dog.sleep();
  dog.bark();
}

This is an example of single inheritance. The class Dog inherits from the class Animal. Let’s take the output of code.

Output

Bruno is eating
Bruno is sleeping
Bruno is barking

code Try Yourself

Hierarchical Inheritance in Dart

Hierarchical Inheritance is the example of our real world example. Our parents may have more than one chidren or our grandparents might have more than one children. All of them carry some of the other properties with each other.

Likewise, if a single class has more than a child then it is called hierarchical inheritance in dart.

This means, If there are class A, class B and class C, and class B inherits from class A and class C inherits also inheritance from class A.

graph TD

A[class A]-->B[class B]
A-->C[class C]

AA[class Animal]-->BB[class Human]
AA-->CC[class Dog]

X[class Dog]-->Y[class GoldenRetriver]
X-->Z[class Labarador]
Y-->yy[class Black]
Y-->zz[class Golden]

Let’s learn hierarchical Inheritance with the following steps:

Step 1: Let’s define the parent class. Here we are defining class name Animal with eat() and sleep() methods and name and age properties.

class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

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

  void sleep() {
    print('$name is sleeping');
  }
}

Step 2: Let’s define child class and inherit the parent classes Dog and Human with their respective methods.

class Dog extends Animal {
  Dog(String name, int age) : super(name, age);

  void bark() {
    print('$name is barking');
  }
}
class Human extends Animal { 
    Human(String name, int age) : super(name, age);
    
    void speak() {
        print('$name is speaking');
    }

}

Step 3: Let’s use the inheritance.

void main() {
  var dog = Dog('Bruno', 2);
  dog.eat();
  dog.sleep();
  dog.bark();
  
  var human = Human('John', 25);
  human.eat();
  human.sleep();
  human.speak();
}

Full Code Example👇


class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

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

  void sleep() {
    print('$name is sleeping');
  }
}

class Dog extends Animal {
  Dog(String name, int age) : super(name, age);

  void bark() {
    print('$name is barking');
  }
}

class Human extends Animal { 
    Human(String name, int age) : super(name, age);
    
    void speak() {
        print('$name is speaking');
    }

}

void main() {
  var dog = Dog('Bruno', 2);
  dog.eat();
  dog.sleep();
  dog.bark();
  
  var human = Human('John', 25);
  human.eat();
  human.sleep();
  human.speak();
}

This is an example of multi-level inheritance. The class Dog inherits from the class Animal and the class Human inherits from the class Animal. Let’s take the output of code.

Output

Bruno is eating
Bruno is sleeping
Bruno is barking

John is eating
John is sleeping
John is speaking

code Try Yourself

Multi-level Inheritance in Dart

Multi-level inheritance is the process of inheriting properties from a parent class and then inheriting properties from the child class.

This means, If there are class A, class B and class C, and class B inherits from class A and class C inherits from class B, then it is called multi-level inheritance.

graph TD
A[class A]-->B[class B]-->BB[class C]


C[class Animal]-->D[class Human]-->DD[class Girl]

E[class Dog]-->F[class GermanShepherd]-->FF[class BlackGermanShepherd]

Example

Let’s follow the following steps to achieve multi-level inheritance in Dart

Step 1: Let’s define the parent class. Here we are defining class name Animal with eat() and sleep() methods and name and age properties.

class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

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

  void sleep() {
    print('$name is sleeping');
  }
}

Step 2: Let’s define child classes Dog and and extend the parent class.

Example

class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

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

  void sleep() {
    print('$name is sleeping');
  }
}

class Dog extends Animal {
  Dog(String name, int age) : super(name, age);

  void bark() {
    print('$name is barking');
  }
} 

class Breed extends Dog {
  String breed;

  Breed(String name, int age, this.breed) : super(name, age);

  void displayBreed() {
    print('$name is a $breed');
  }
} 

void main() {
  var dog = Breed('Bruno', 2, 'Labrador');
  dog.eat();
  dog.sleep();
  dog.bark();
  dog.displayBreed();
}

Output

Bruno is eating
Bruno is sleeping
Bruno is barking
Bruno is a Labrador

code Try Yourself

Quiz