Dart is a statically-typed, object-oriented programming language that is widely used for developing cross-platform mobile and web applications. One of the key features of Dart is its access modifiers, which allow developers to control the visibility and accessibility of class members. Access modifiers provide a way to protect the internal state of an object and prevent unwanted modifications.
In this blog post, we'll explore the four types of access modifiers in Dart and how to use them.
Types of Access Modifiers in Dart
Dart supports four types of access modifiers:
1.public
2.private
3.protected
4.extension
Let's take a closer look at each of these access modifiers.
1. Public Access Modifier
The public access modifier is the default access level in Dart. It is applied to all class members that do not have an explicit access modifier. A public class member can be accessed from any code within the same package or library.
Here's an example:
class Person {
String name;
int age;
void greet() {
print('Hello, my name is $name and I am $age years old.');
}
}
void main() {
var person = Person();
person.name = 'Nachiketa';
person.age = 30;
person.greet(); // Hello, my name is Nachiketa and I am 30 years old.
}
In this example, the name, age, and greet() members of the Person class are all public. They can be accessed from the main() function, which is also in the same package.
2. Private Access Modifier
The private access modifier is used to restrict access to class members within the same library. A private class member is denoted by prefixing its name with an underscore character (_).
Here's an example:
class Person {
String _name;
int _age;
void _greet() {
print('Hello, my name is $_name and I am $_age years old.');
}
void introduce() {
_name = 'Nachiketa';
_age = 30;
_greet(); // Hello, my name is Nachiketa and I am 30 years old.
}
}
void main() {
var person = Person();
person.introduce();
}
In this example, the name, age, and greet() members of the Person class are all private. They can only be accessed from within the Person class. The introduce() method is public and can be accessed from the main() function, which is also in the same library.
3. Protected Access Modifier
The protected access modifier is not officially supported in Dart. However, it can be simulated using the package access modifier. A package class member can be accessed from any code within the same package, but not from other packages.
Here's an example:
library my_package;
class Person {
String name;
int age;
void _greet() {
print('Hello, my name is $name and I am $age years old.');
}
void introduce() {
name = 'Nachiketa';
age = 30;
_greet(); // Hello, my name is Nachiketa and I am 30 years old.
}
}
In this example, the name and age members of the Person class are public. The _greet() method is protected and is denoted by prefixing its name with an underscore character (_). It can be accessed from within the Person class and any code within the same package, but not from other packages. The introduce() method is public and can be accessed from any code within the same package.
4. Extension Access Modifier
The extension access modifier was introduced in Dart 2.6 and is used to add functionality to an existing class without having to modify the class itself. An extension can add new methods and properties to a class, even if the class is final or comes from a third-party library.
Here's an example:
extension IntExtensions on int {
bool isEven() => this % 2 == 0;
bool isOdd() => this % 2 == 1;
}
void main() {
var number = 3;
print(number.isEven()); // false
print(number.isOdd()); // true
}
In this example, we create an extension called IntExtensions on the int class. The extension adds two new methods isEven() and isOdd() that can be used on any int value.
Conclusion
Access modifiers are a powerful feature of Dart that allow developers to control the visibility and accessibility of class members. They provide a way to protect the internal state of an object and prevent unwanted modifications. By using access modifiers appropriately, you can write code that is more secure, maintainable, and extensible.
Thankyou
- Nachiketa
3 Comments
poi
ReplyDeletebvf
Deleteoikkk
ReplyDelete