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.
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.
class A{
...
}
class B extends A{
...
}
class C extends B{
...
}
The figure below shows the visual representation of Multi-level inheritance in Dart:
The following code shows the implementation of the multi-level inheritance:
class Shape{void display(){// creating methodprint("This is Shape class");}}class Triangle extends Shape{// creating methodvoid showInfo(){print("This is Triangle class");}}class Circle extends Triangle{// creating methodvoid showMessage(){print("This is Circle class");}}void main(){// Creating an object of class Circlevar circle = Circle();// invoking Circle's method using Circle's objectcircle.showMessage();// invoking Triange's method using Circle's objectcircle.showInfo();// invoking Shape's method using Circle's objectcircle.display();}
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.