MultiLevel Inheritance in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

Multilevel inheritance in dart is the case when different classes are inheriting in a form of chain, i.e., one class extends some parent class, and the other class extends the class that was extending the parent class.

The syntactical representation of multilevel inheritance looks something like this −

class A {}
class B extends A {}
class C extends B {}

If we notice the above syntax, we can clearly see that the class A is the parent class for the class B, which is extending it. Also, the class B acts as a parent for class C, which is extending class B.

Multilevel inheritance is nothing but a chaining of inheritance.

Example

Let's consider an example, where we make use of different classes to form a multilevel inheritance in a dart program.

Consider the example shown below −

 Live Demo

class Wood{
   void printName(){
      print("Inside class Wood");
   }
}

class Table extends Wood{
   void printTable(){
      print("Inside Table class");
   }
}

class TableLegs extends Table{
   void printTableLegs(){
      print("Inside TableLegs class");
   }
}

void main(){
   TableLegs tl = new TableLegs();
   tl.printTableLegs();
   tl.printTable();
   tl.printName();
}

In the above example, we have three different classes, namely Wood, Table, and TableLegs. Inside the main function, we create an object of the TableLegs class and then invoke the methods of the parent class that the class extends.

Output

Inside TableLegs class
Inside Table class
Inside class Wood
raja
Published on 21-May-2021 12:59:00
Advertisements