Encapsulation_Dart
import "dart:io";
/// Wrapping data in a single unit through access modifier it is called as Encapsulation
/// Example 1
// class A {
// var name = "Dart";
// var _amount = 0; // private variable(member)
//
// void getData(int amt) {
// _amount = amt;
// }
// int showData() {
// return _amount;
// }
// }
// void main() {
// A obj = new A();
// obj.getData(500);
// print(obj.showData());
// }
/// Example 2
/// In this example, we will create a class named Employee.
/// The class will have two private properties _id and _name.
/// We will also create two public methods getId() and getName() to access the private properties.
/// We will also create two public methods setId() and setName() to update the private properties.
// class Employee {
// // Private properties
// int? _id;
// String? _name;
//
// //Getter method to access private property _id
// int getId() {
// return _id!;
// }
// //Getter method to access private property _name
// String getName() {
// return _name!;
// }
// // Setter method to update private property _id
// void setId(int id) {
// this._id = id;
// }
// // Setter method to update private property _name
// void setName(String name) {
// this._name = name;
// }
// }
// void main() {
// Employee emp = new Employee();
//
// emp.setId(1);
// emp.setName("John");
//
// print("Id is : ${emp.getId()} ");
// print("Name is : ${emp.getName()}");
// }
/// Example 3
/// In this example,we will create a class named Employee1. The class has one private property _name.
/// We will also create a public method getName() to access the private property.
// class Employee1 {
// // Private property
// var _name;
//
// //Getter method used to access private property _name
// String getName() {
// return _name;
// }
// //Setter method used to update private propety _name
// void setName(String name) {
// this._name = name;
// }
// }
// void main() {
// Employee1 emp1 = new Employee1();
//
// emp1.setName("Jack");
// print("Name is : ${emp1.getName()}");
// }
/// Example 4
// class Student {
// final _schoolName = "ABC School";
//
// String getSchoolName() {
// return _schoolName;
// }
// }
// void main() {
// Student student = new Student();
//
// print(student.getSchoolName());
// // This is not possible
// // student._schoolName = "DEF School";
// }
/// Example 5
/// You can create getter and setter methods by using the get and set keywords.
/// In this example below, we have created a class named Vehicle.
/// The class has two private properties _model and _year. We have also created two getter
/// and setter methods for each property. The getter and setter methods are named model and year.
/// The getter and setter methods are used to access and update the value of the private properties.
class Vehicle {
String? _model;
int? _year;
// Getter method
String get model {
return _model!;
}
// Setter method
set model(String model) {
_model = model;
}
// Getter method
int get year {
return _year!;
}
// Setter method
set year(int year) {
_year = year;
}
}
void main() {
Vehicle vehicle = new Vehicle();
vehicle.model = "Toyota";
vehicle.year = 2019;
print(vehicle.model);
print(vehicle.year);
}
/// Notes:
/// How To Achieve Encapsulation In Dart?
// Encapsulation can be achieved by:
//
// Declaring the class properties as private by using underscore(_).
// Providing public getter and setter methods to access and update the value of private property.
///Note:
//Dart doesn’t support keywords like public, private, and protected.
// Dart uses _ (underscore) to make a property or method private.
// The encapsulation happens at library level, not at class level.
/// Getter and Setter Methods
// Getter and setter methods are used to access and update the value of private property.
// Getter methods are used to access the value of private property.
// Setter methods are used to update the value of private property.
/// Reason
//The reason is that using underscore (_) before a variable or method name makes it library private
// not class private. It means that the variable or method is only visible to the library
// in which it is declared. It is not visible to any other library.
// In simple words, library is one file. If you write the main method in a separate file,
// this will not work.
/// Read-only Properties
// You can control the properties’s access and implement the encapsulation in the dart
// by using the read-only properties. You can do that by adding the final keyword before
// the properties declaration. Hence, you can only access its value, but you cannot change it.
/// Note:
// Properties declared with the final keyword must be initialized at the time of declaration.
// You can also initialize them in the constructor.
/// How To Create Getter and Setter Methods?
// You can create getter and setter methods by using the get and set keywords.
// In this example below, we have created a class named Vehicle.
// The class has two private properties _model and _year. We have also created two getter
// and setter methods for each property. The getter and setter methods are named model and year.
// The getter and setter methods are used to access and update the value of the private properties.
/// Note:
// In dart, any identifier like (class, class properties, top-level function, or variable)
// that starts with an underscore _ it is private to its library.
/// Why Encapsulation Is Important?
/// Data Hiding:
// Encapsulation hides the data from the outside world. It prevents the data from being accessed
// by the code outside the class. This is known as data hiding.
/// Testability:
// Encapsulation allows you to test the class in isolation. It will enable you to test
// the class without testing the code outside the class.
/// Flexibility:
// Encapsulation allows you to change the implementation of the class without affecting
// the code outside the class.
/// Security:
// Encapsulation allows you to restrict access to the class members. It will enable you
// to limit access to the class members from the code outside the library
Comments
Post a Comment