Hierarchical Inheritance in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

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
raja
Published on 21-May-2021 12:50:02
Advertisements