Dart: Private Instance Variable

Dart: Private Instance Variable

ยท

1 min read

  • For creating private Instance variable we have to use " _ " before variable name.

  • This kind of Instance exists in only library. No one can access this Library from outside.

    • First we have to create one library for this we use " library " keyword.

      Then we create one class which is " class A ".

    • In Class A we passed one variable called _privateInstanceVariable = 20.

    • In class A we create on function call display and pass one print statement on this function we which we called in our other file .

  •   library private_instance_variable;
    
      class A 
      {
       var _privateInstanceVariable = 20;
    
        void display()
        {
          print("The Private Instance Variable value is : $_privateInstanceVariable ");
        }
      }
    
  • This file we called in our second file using import keyword , and then we extend class A on this file using extend keyword.

  • If we try to change value of obj B than we face error on this. because obj get value from class B where Class B get value from Class A from other file and this is private Instance variable so we can not change the value of obj.

import "136_Dart_Private_Instance_Variable.dart";


void main()
{
 var obj = B();
 obj.display();

}

class B extends A{}

ย