Constructor: Invoking a non-default superclass constructor

Invoking a non-default superclass constructor

class Person { 
   String firstName; 

   Person.fromJson(Map data) { 
   print('in Person'); 
   } 
} 

class Employee extends Person { 
   // Person does not have a default constructor; you must call super.fromJson(data). 
   Employee.fromJson(Map data) : super.fromJson(data) { 
   print('in Employee'); 
   } 
} 

main() { 
   var emp = new Employee.fromJson({}); 

   // Prints: 
   // in Person 
   // in Employee 
   if (emp is Person) { 
      // Type check 
      emp.firstName = 'Bob'; 
   } 
   (emp as Person).firstName = 'Bob'; 
}

Related concepts

Invoking a non-default superclass constructor

Constructor: Invoking a non-default superclass constructor — Structure map

Clickable & Draggable!

Constructor: Invoking a non-default superclass constructor — Related pages: