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

Was this helpful?

  1. Dart Fundamentals-2
  2. Constructor
  3. Parameterised Constructor

Basic Parameterised Constructor & "this" keyword -2

PreviousParameterised ConstructorNextParameterised Constructor with Syntactic sugar

Last updated 4 years ago

Was this helpful?

Due to naming conflict we use "this" keyword inside parameterised constructor. If there is no naming conflict you can remove the "this" keyword . But it's good practice to always use the "this" keyword

void main() {
  // Parameterised Constructor 

  
  //var employee1 = Employee();
  
  
  var employee1 = Employee(1, "John", "Doe", "CTO", 12);

  print(employee1.id);
  print(employee1.firstname);
  print(employee1.lastname);
  print(employee1.position);
  print(employee1.number_of_day_worked);
}

class Employee {
  int id;

  String firstname;

  String lastname;

  String position;

  int number_of_day_worked;
  
  
  


  //  Parameterised Constructor - 1
  
  
 
   Employee(id, firstname, lastname, position, number_of_day_worked) {
    this.id = id;
    this.firstname = firstname;
    this.lastname = lastname;
    this.position = position;
    this.number_of_day_worked = number_of_day_worked;
   }
  
  
  
  
    

  //  Parameterised Constructor   - we avoid the following to be consistent  

//    Employee(a, b, c, d, e) {
//     id = a;
//     firstname = b;
//     lastname = c;
//     position = d;
//     number_of_day_worked = e;
//    }

  

}

The values passed in the parameterised constructor are assigned to the instance variables.

So, Parameterised constructor creates the object and also sets the instance variables

You can't use default constructor and parameterised constructor in the same class definition .