Hierarchical Inheritance in Dart Programming


Hierarchical inheritance is the case of inheritance when two classes inherit a single class.

The syntactic representation of a hierarchical inheritance looks something like this −

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

In the above syntactic representation, we can see that two classes, namely B and C are inheriting (or extending) class A.

Example

Let's consider an example of hierarchical inheritance in dart. Consider the example shown below −

 Live Demo

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

class Daughter extends Parent{
   void age(age){
      print("Her age is: ${age}");
   }
}

class Son extends Parent{
   void name(name){
      print("My name is: ${name}");
   }
}

void main(){
   Daughter d = new Daughter();
   d.printName();
   d.age(23);

   Son s = new Son();
   s.printName();
   s.name("Tuts");
}

In the above example, we have two classes, namely Son and Daughter that are extending the class named Parent.

Output

Inside class Parent
Her age is: 23
Inside class Parent
My name is: Tuts

Updated on: 21-May-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements