Hi am pretty new to Object oriented programming. I want to know what are matching mutator methods and how does one use them and where? what is the use of a mutator method? Explanation through an example will be preferred.![]()
Hi am pretty new to Object oriented programming. I want to know what are matching mutator methods and how does one use them and where? what is the use of a mutator method? Explanation through an example will be preferred.![]()
Mutator method is a method used to control the changes made to a variable. they are usually used in Object Oriented Programming(OOP) for encapsulation purposes. There are two mutators as such, One called "setter" which sets some value to a variable and other is "getter" which gets the value of a variable. The mutators can be used in programming languages other than the Object oriented ones. the difference being that in OOP languages, they are exclusively used for encapsulation purposes, where we want the variable hidden from changes in the outside code. An example of a code using the setters and getters in a java program is given below;
Hope you got what you asked for. In case of further querries, please so reply.Code:public class Test { private String name; /** *@return the name */ public String getName() { return name; } /** * newName is the name to set */ public void setName(String newName) { name = newName; } }
A mutator is basically a method that changes a variable inside a class, variables that are either private or protected. This is what a mutator is technically speaking, but what you are referring to, that is a matching mutator method is what we call "setters" which set values to a private or protected variable like in the code below, where we are setting the name of a "newStudentName" to the "studentName";
In case you did not get it, let me know about it.Code:public void setNewStudent(String newStudentName) { this.studentName = newStudentName; }
Well i thought this further information would be helpful for people who are learning C# and not Java. In C# there are properties which are special type of class members, there are no explicit methods unlike Java. a code that gives the get and set method is given below;
Code:public class Test { private int num; /// gets or sets a student's name public int Number { get { return num; } set { num = 5; } } /// i have set the num to value 5, which can be any number }
Bookmarks