Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

#dart
inheritance
communitycreator

What is hierarchical inheritance in Dart?

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Dart inheritance

Inheritance refers to a class’s ability to derive properties and behaviors from another class. Dart allows one class to inherit from another, and generate a new class from an existing one. To do this, we use the extend keyword with any class to extend the functionality of a superclass.

Hierarchical inheritance

Hierarchical inheritance refers to the case where two classes inherit from a single class (parent class).

Inheritance terminology

  • Parent class: This is the class from which the child class inherits its properties. It is also known as the “base class” or “superclass.”
  • Child class: This is the class that inherits the properties of another class. It is also known as a “subclass” or “derived class”.

Syntax

The following is the required syntax:

class parent_class{
...
}

class child1_class extends parent_class{
...
}

class child2_class extends parent_class{
...
}

Below is the visual representation of hierarchical inheritance in Dart:

Visual representation of hierarchical inheritance in Dart

Example

The following code shows how to implement hierarchical inheritance in Dart.

// Dart program to show hierachical inheritance
// Creating a parent class
class Animal{
// Creating an attribute
String breed;
// Creating a function
void display(){
print("This is the Animal class called Parent class");
}
}
// Creating Child class
class Cat extends Animal{
// Creating a function
void meow(){
print("$breed meow everytime.");
}
}
// Creating another child class
class Dog extends Animal{
// Creating a function
void bark(){
print("$breed barks !!!");
}
}
void main() {
// Creating an object of class Cat
var cat = Cat();
cat.breed = "Maine Coon";
cat.meow();
// Creating an object of class Dog
var dog = Dog();
dog.breed = "Dachsador";
dog.bark();
// Creating an object of the superclass Animal
var animal = Animal();
animal.display();
}
Dart program to show hierarchical inheritance

Explanation

  • Lines 5 to 12: We create the parent class Animal with its attribute breed and function display().

  • Lines 14 to 20: We create the child class Cat with its function meow(). It inherits the breed property of the parent class.

  • Lines 23 to 28: We create a child class Dog with its function bark(). This also inherits the breed property of the parent class.

  • In the main drive, we create objects of class Cat, Dog, and Animal. Finally, we assign value to the breed attribute using the class’s object, and invoke the function.

Note: Both class Cat and Dog inherit from class Animal, demonstrating hierarchical inheritance.

RELATED TAGS

#dart
inheritance
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring