Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems.... , a mutator method is a method
Method (computer science)
In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value... used to control changes to a variable.
Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs.... , in keeping with the principle of encapsulation
Encapsulation (computer science)
In computer science, Encapsulation is the hiding of the internal mechanisms and data structures of a software component behind a defined interface, in such a way that users of the component only need to know what the component does, and cannot make themselves dependent on the details of how it does it.... . According to this principle, member variable
Variable
A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e.... s of a class
Class (computer science)
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share.... are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.
Often a "setter" is accompanied by a "getter" (also known as an accessor), which returns the value of the private member variable.
Mutator methods may also be used in non-object-oriented environments.
Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems.... , a mutator method is a method
Method (computer science)
In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value... used to control changes to a variable.
Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs.... , in keeping with the principle of encapsulation
Encapsulation (computer science)
In computer science, Encapsulation is the hiding of the internal mechanisms and data structures of a software component behind a defined interface, in such a way that users of the component only need to know what the component does, and cannot make themselves dependent on the details of how it does it.... . According to this principle, member variable
Variable
A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e.... s of a class
Class (computer science)
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share.... are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.
Often a "setter" is accompanied by a "getter" (also known as an accessor), which returns the value of the private member variable.
Mutator methods may also be used in non-object-oriented environments. In this case, a reference to the variable to be modified is passed to the mutator, along with the new value. In this scenario, the data is not protected from changes that bypass the mutator method, whose role becomes simply to validate the input, and the onus falls to the developer
Software developer
A software developer is a person or organization concerned with facets of the software development process wider than design and coding, a somewhat broader scope of computer programming or a specialty of project manager including some aspects of Software product management.... to ensure the variable isn't modified directly.
In programming languages that support them,
Properties
Property (programming)
In some object-oriented programming programming languages, a property is a special sort of Class member, intermediate between a field and a method .... offer a convenient alternative without giving up the utility of encapsulation.
Smalltalk example
age: aNumber
" Set the receiver age to be aNumber if is greater than 0 and less than 150 "
(aNumber between: 0 and: 150)
ifTrue: [ age := aNumber ]
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share.... representing a student with only the name stored, one can see the variable
Variable
A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e.... name is private, i.e. only visible from the Student class, and the "setter" and "getter" is public, namely the "getName" and "setName(name)" methods.
public class Student
C# example
This example illustrates the C# idea of properties
Property (programming)
In some object-oriented programming programming languages, a property is a special sort of Class member, intermediate between a field and a method .... , which are a special type of class
Class (computer science)
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share.... member. Unlike Java, no explicit methods are defined that handle the "getting" and "setting" of the private member variable. Rather, a public property is exposed for the private variable which defines logic to handle such actions.
public class Student
Delphi Example
This is a simple class in Delphi language that illustrates the concept of public property that access a private field. In the SetName method additionally you can validate the input or take another action like trigger an event.
interface
type
TStudent = class
private
fName: string;
procedure SetName(const Value: String);
public
///
/// Set or get the name of the student.
///
property Name:String read fName write SetName;
end;
implementation
procedure TStudent.Setname(const Value: String);
begin
fName := Value;
end;