What do we understand by the word “encapsulation”? It is a mechanism to bind something together and keep it safe from outside interference and misuse.
In programming, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:
Familiar with the topic? Play Quiz
In simple words, if you want to limit the access of any classes or methods or variables, you can use encapsulation.
In Dart, encapsulation is done by using the keywords public
, private
and protected
.
Modifier | Description |
---|---|
public | The default, visible everywhere |
private | Visible only in the the current classes or files based on the scenerio |
Dart doesn’t have a protected
keyword, but it does have a concept of libraries that can help you achieve the same effect.
If you don’t specify any modifier, it is by default public
.
class Person {
String name;
int age;
}
If you want to make any class, method or variable private, you can use the underscore _
symbol.
class Person {
String _name;
int _age;
}
Public Varibales
Public varibales are those which are accessible from anywhere in the program or project. We need the import (path) of the file and that will be accessible.
int age = 10;
String name = "John Doe";
Private Varibales
Private varibales are those which are accessible only from the same file. We can’t access them from other files.
int _age = 10;
String _name = "John Doe";
Note that, if you try to access the private varibales from other files, you will get an error.
Note:
If we can’t access the private varibales, why create them? Well, they are useful in many ways.
Other than that, you can use private varibales to make your code more readable and optimized by hiding the implementation details.
We can access the private variables with getters and setters.
We learned about the encapsulation which limits the access of variables and methods from outside the class. But sometimes we need to access the private variables from outside the class. In this case, we use getters and setters.
Getters and setters are special methods that provide read and write access to an object’s properties.
Why do we need getters and setters ?
Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value.
How to create getters and setters ?
To create a getter and setter, we use the get and set keywords. Rules for creating getters and setters are as follows:
Now, following the above rules, let’s create a getter and setter for a class.
class Student {
String name;
int age;
String get studentName {
return name;
}
void set studentName(String name) {
this.name = name;
}
int get studentAge {
return age;
}
void set studentAge(int age) {
this.age = age;
}
}
void main() {
var student = Student();
student.studentName = 'Peter';
student.studentAge = 20;
print(student.studentName);
print(student.studentAge);
}
Output
Peter
20
In the above example, we have created a class named Student. The class has two instance variables name and age. We have created getters and setters for these variables.
Note that, setters is not a function here it is a varibale that takes a value and assigns it to the instance variable.
Dart provides a shorthand syntax for getters and setters.
class Student {
String name;
int age;
String get studentName => name;
void set studentName(String name) => this.name = name;
int get studentAge => age;
void set studentAge(int age) => this.age = age;
}
void main () {
var student = Student();
student.studentName = 'Peter';
student.studentAge = 20;
print(student.studentName);
print(student.studentAge);
}
Output
Peter
20