Intermediate Dart
  • Welcome to the Course
  • Course Overview
  • Resources
  • Dart Fundamentals-2
    • Section Overview
    • Basic Concepts
      • Class
      • Object/Instance
      • Instance /Field Variables
      • Methods
      • "this" keyword-1
    • Constructor
      • Default Constructor
      • Parameterised Constructor
        • Basic Parameterised Constructor & "this" keyword -2
        • Parameterised Constructor with Syntactic sugar
        • Optional Arguments in Parameterised Constructor
      • Named Constructor
    • Getters & Setters and Encapsulation
      • Access Modifiers and _ symbol
      • Implicit / Default Getters & Setters
      • Custom Getters and Setters
      • Encapsulation
    • Inheritance
      • Inheritance, Types of Inheritance , Terminology
      • Method Overriding
      • @override annotation
      • super Keyword -1
      • super Keyword -2
    • Polymorphism
    • Static Variables and Methods
    • Abstract Class and Abstract Methods
    • Interface
    • Miscellaneous concepts
      • Untitled
      • Object Class
      • Mixin
      • Immutable Instances
      • Cascade notation
  • Dart Fundamentals -3
    • Section Overview
    • Asynchronous Programming
      • Basic Concept of Asynchronous programming
      • async , await , then
      • Exception Handling in asynchronous code
    • Functional Programming
      • Anonymous functions
      • High Level Iterators
    • Advanced Asynchronous Programming
      • Isolates
      • Streams
      • Generators
      • Microtask and Zones
    • Method & Constructor Overloading
Powered by GitBook
On this page
  • Polymorphic Array
  • Polymorphic arguments

Was this helpful?

  1. Dart Fundamentals-2

Polymorphism

Previoussuper Keyword -2NextStatic Variables and Methods

Last updated 4 years ago

Was this helpful?

Polymorphic Array

void main() {
  
  // Polymorphism 

  Employee e1 = Developer(10);
  
  Employee e2 = Developer(20);
  

  Employee e3 = Manager(30);
  
  Employee e4 = Manager(40); 
  
  
  
  List<Employee> organization = [e1,e2,e3,e4]; 
  
  
  
  for(var employee in organization) {
    
    
     print(employee.salary(20));
      
    }
  
  

}

class Employee {

  int dayworked;

  int salary(int perDaywage) {
    return (this.dayworked * perDaywage);
  }
  
  Employee(this.dayworked);
}




// Developer Class
class Developer extends Employee {
  
  
  @override 
  int salary (int perDaywage){
    
    return (this.dayworked*perDaywage+80);
  }
  
  
  Developer(int dayworked):super(dayworked);
  

  
}





// Manager Class

class Manager extends Employee {
  
  
  @override 
  int salary (int perDaywage){
    
    return (this.dayworked*perDaywage+30);
  }
  
   Manager(int dayworked):super(dayworked);
}

Polymorphic arguments

void main() {
  
  // Polymorphism => Polymorphic argument 
  
  int calsalary(Employee employee){
     
     return employee.salary(20);
    
  }
     
   print(calsalary(Manager(10)));
  

}

class Employee {

  int dayworked;

  int salary(int perDaywage) {
    return (this.dayworked * perDaywage);
  }
  
  Employee(this.dayworked);
}




// Developer Class
class Developer extends Employee {
  
  
  @override 
  int salary (int perDaywage){
    
    return (this.dayworked*perDaywage+80);
  }
  
  
  Developer(int dayworked):super(dayworked);
  
}

// Manager Class

class Manager extends Employee {
  
  
  @override 
  int salary (int perDaywage){
    
    return (this.dayworked*perDaywage+30);
  }
  
   Manager(int dayworked):super(dayworked);
}

As Dart has type inference , if you don't explicitly write the datatype , you won't notice how polymorphism is working . For this reason all the codes in this section has explicit datatype . If you take advantage of "type inference" , the polymorphism will still exits , but will seem less "magical"