DART CLASSES - DEFUALT, PARAMETER, NAME CONSTRUCTOR - OOP TUTORIALS

preview_player
Показать описание
What is constructor?

A constructor is a different type of function which is created with same name as its class name. The constructor is used to initialize an object when it is created. When we create the object of class, then constructor is automatically called. It is quite similar to class function but it has no explicit return type. The generative constructor is the most general form of the constructor, which is used to create a new instance of a class.

It is option to declare within the class. All class have own constructor but if we don't declare or forget then Dart compiler will create default constructor automatically by passing the default value to the member variable. If we declare own constructor, then default constructor will be ignored.

Types of Constructors

There are three types of constructors in Dart as given below.

Default Constructor or no-arg Constructor
Parameter Constructor
Named Constructor

Default Constructor or no-arg Constructor

A Constructor which has no parameter is called default constructor or no-arg constructor. It is automatically created (with no argument) by Dart compiler if we don't declare in the class. The Dart compiler ignores the default constructor if we create a constructor with argument or no argument.

Parameterized Constructor

We can also pass the parameters to a constructor that type of constructor is called parameterized constructor. It is used to initialize instance variables. Sometimes, we need a constructor which accepts single or multiple parameters. The parameterized constructors are mainly used to initialize instance variable with own values.

Named Constructors

The named constructors are used to declare the multiple constructors in single class.

#CONTRUCTOR #CLASS #DART #PARAMETER #DEFAULT #CODINGDART #TUTORIAL #FLUTTER #OOP #CODEFAST #DARTFORFLUTTER
Рекомендации по теме