loading

Getters and Setters

The unique class methods known as getters and setters are used to read and write access to an object’s properties. The setter method is used to set or initialize the appropriate class fields, while the getter function is used to get or retrieve the value of the variable. All classes have getter and setter methods assigned to them by default. By explicitly declaring the getter and setter methods, we can, nevertheless, override the default ones.

Defining a getter

By utilizing the get keyword without a parameter and a proper return type, we may define the getters method.

Syntax

				
					return_type get field_name{  
}  
				
			

Defining a setter

The set keyword can be used to declare the setter method with a single parameter and no return type.

Syntax

				
					set field_name {  
}  
				
			

Example

				
					class Student {  
          String stdName;  
          String branch;  
          int stdAge;  
 // getter method   
         String get std_name   
             {  
             return stdName;  
             }  
         void set std_name(String name)  
                 {  
              this.stdName = name;  
  
               }  
          void set std_age(int age) {  
               if(age> = 20){  
                   print("Student age should be greater than 20")  
                     }else{   
                         this.stdAge = age;  
                              }  
                      }  
  
                                                         }  
           int get std_age{  
                 return stdAge;  
  
}  
        void set std_branch(String branch_name) {  
                 this.branch = branch_name;  
  
}  
     int get std_branch{  
            return branch;  
}  
  
}  
void main(){  
Student std = new Student();  
std.std_name = 'John';  
std.std_age = 24;  
std.std_branch = 'Computer Science';  
  
print("Student name is: ${std_name}");  
print("Student age is: ${std_age}");  
print("Student branch is: ${std_branch}");  
}  
				
			

Output

				
					Student name is: John
Student age is: 24
Student Branch is: Computer Science
				
			

Example

				
					class Car {  
  String makedate;  
  String modelname;  
  int manufactureYear;  
  int carAge;  
  String color;  
// Getter method  
  int get age {  
    return carAge;  
  }  
// Setter Method  
  void set age(int currentYear) {  
    carAge = currentYear - manufactureYear;  
  }  
// defining parameterized constructor  
  Car({this.makedate,this.modelname,this.manufactureYear,this.color,});  
}  
//Age here is both a getter and a setter. Let's see how we can use it.  
void main() {  
 Car c =   
 Car(makedate:"Renault 20/03/2010",modelname:"Duster",manufactureYear:2010,color:"White");  
  print("The car company is: ${c.makedate}");   
  print("The modelname is: ${c.modelname}");   
  print("The color is:${c.color}");  
  c.age = 2020;  
  print(c.age);  
}  
				
			

Output

				
					The car company is: Honda 20/03/2010
The modelname is: City
The color is: White
10
				
			

Explanation

The getter and setter methods are defined before the constructor in the code above.

Share this Doc

Getters and Setters

Or copy link

Explore Topic