FuzzDuck
FuzzDuck
Getter and setter
FuzzDuck
Getter and setter

Getter and Setter

Getters and setter are class methods, used to define a specific parameter or retrieve its value.

The getter and setter methods are used when you want to get a specific parameter or change it in a class. The parameter itself can be private and here the getter and setter class are used to gain access through methods as calling the parameters directly is denied due to encapsulation. When we call the get method the specific value we call for are returned to us. When we call the set method, we can alter this value to a new one. Some of the pros of the getter and setter methods are that they give more control over the access of the data, they make it easier to add more functionality later on and much more.

Code Example - Declaring class with getter and setter methods
// --- Declares the beginning of the class --- //
public class Duck {

 // --- Declares the variables inside the class --- //
 private int age = 5;
 private String name = "Ducky";

 // --- Getter method, returning the object's variable "age" --- //
 public int getAge(){
  return this.age;
 }

 // --- Setter method, setting the object's variable "age" to new value --- //
 public void setAge(int newAge) {
  this.age = newAge;
 }

 // --- Getter method, returning the object's variable "name" --- //
 public String getName(){
  return this.name;
 }

 // --- Setter method, setting the object's variable "name" to new value --- //
 public void setName(String newName) {
  this.name = newName;
 }

}
// --- Declares the beginning of the class --- //
public class Duck {

 // --- Declares the variables inside the class --- //
 private int age = 5;
 private String name = "Ducky";

 // --- Getter method, returning the object's variable "age" --- //
 public int getAge(){
  return this.age;
 }

 // --- Setter method, setting the object's variable "age" to new value --- //
 public void setAge(int newAge) {
  this.age = newAge;
 }

 // --- Getter method, returning the object's variable "name" --- //
 public String getName(){
  return this.name;
 }

 // --- Setter method, setting the object's variable "name" to new value --- //
 public void setName(String newName) {
  this.name = newName;
 }

}
// --- Declares the beginning of the class --- //
class Duck {

 // --- Declares the variables inside the class --- //
 public $age = 5;
 public $name = "Ducky";

 // --- Getter method, returning the object's variable "age" --- //
 public function getAge() {
  return $this->age;
 }

 // --- Setter method, setting the object's variable "age" to new value --- //
 public function setAge($newAge) {
  $this->age = $newAge;
 }

 // --- Getter method, returning the object's variable "name" --- //
 public function getName() {
  return $this->name;
 }

 // --- Setter method, setting the object's variable "name" to new value --- //
 public function setName($newName) {
  $this->name = $newName;
 }

}
Code Example - Instantiating class, retrieving and modifying variables with getter and setter methods.
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args){

 // --- Instantiating a Duck instance called 'ducky' --- //
 Duck ducky = new Duck();

 // --- Printing ducky's age --- //
 Console.WriteLine(ducky.getAge());

 // --- Printing ducky's name --- //
 Console.WriteLine(ducky.getName());

 // --- Setting new age for our ducky object --- //
 ducky.setAge(10);

 // --- Setting new name for our ducky object --- //
 ducky.setName("Dan Duck");

 // --- Printing ducky's new age --- //
 Console.WriteLine(ducky.getAge());

 // --- Printing ducky's new name --- //
 Console.WriteLine(ducky.getName());

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String[] args) {

 // --- Instantiating a Duck instance called 'ducky' --- //
 Duck ducky = new Duck();

 // --- Printing ducky's age --- //
 System.out.println(ducky.getAge());

 // --- Printing ducky's name --- //
 System.out.println(ducky.getName());

 // --- Setting new age for our ducky object --- //
 ducky.setAge(10);

 // --- Setting new name for our ducky object --- //
 ducky.setName("Dan Duck");

 // --- Printing ducky's new age --- //
 System.out.println(ducky.getAge());

 // --- Printing ducky's new name --- //
 System.out.println(ducky.getName());

}
// --- Instantiating a Duck instance called 'ducky' --- //
$ducky = new Duck ();

// --- Printing ducky's age --- //
echo $ducky->getAge();

// --- Printing ducky's name --- //
echo $ducky->getName();

// --- Setting new age for our ducky object --- //
$ducky->setAge(10);

// --- Setting new name for our ducky object --- //
$ducky->setName("Dan Duck");

// --- Printing ducky's new age --- //
echo $ducky->getAge();

// --- Printing ducky's new name --- //
echo $ducky->getName();