Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

inheritance
multi-level
dart
communitycreator

What is Multi-level 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 capability to derive properties and behaviors from another class.

Dart allows one class to inherit from another, generating a new class from an existing one. We use the extend keyword to inherit from another class.

Multi-level inheritance

In Dart, multi-level inheritance occurs when various classes inherit in a chain (i.e., one class extends a parent class, and another class extends the class that extended the parent class). A subclass is inherited by another subclass or forms an inheritance chain.

Terminology in Inheritance

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

Syntax

class A{
...
}

class B extends A{
...
}

class C extends B{
...
}

Figure

The figure below shows the visual representation of Multi-level inheritance in Dart:

Visual representation of Multi-level inheritance

Code

The following code shows the implementation of the multi-level inheritance:

class Shape{
void display(){
// creating method
print("This is Shape class");
}
}
class Triangle extends Shape{
// creating method
void showInfo(){
print("This is Triangle class");
}
}
class Circle extends Triangle{
// creating method
void showMessage(){
print("This is Circle class");
}
}
void main(){
// Creating an object of class Circle
var circle = Circle();
// invoking Circle's method using Circle's object
circle.showMessage();
// invoking Triange's method using Circle's object
circle.showInfo();
// invoking Shape's method using Circle's object
circle.display();
}
Code for Multi-level inheritance in Dart

Code explanation

We create three different classes named Shape, Triangle, and Circle with methods display(), showInfo(), and showMessage(), respectively. In the main drive, we create an object of the Circle class and then invoke the methods of the parent class that the class extends.

Note: Dart does not support multiple inheritances.

RELATED TAGS

inheritance
multi-level
dart
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