answersLogoWhite

0


Best Answer

Accessors and mutators are advantageous when you want to run some code as part of setting or retrieving a member. A classic example is a class for a window in a program. You might want to store the left,right,top,bottom values, but you also want methods that set the width and height. Accessors/mutators let you store the data any way you want (left,right,top,bottom or left,right,height,width) and keep the interface the same. In fact, a future version of the class might CHANGE the way you store the data. If you're using accessors and mutators, then you can do this without requiring class's users to change also.

Continuing with the window, when you set any of these properties, you want the window to resize automatically. So there might be a lot of code that gets run.The disadvantage is that they add some complexity.

a.setsize(b.getsize())

is a lot uglier than

a.size = b.size

and it takes more typing. And all the accessors and mutators take a lot of space in the class declaration. And if the accessors/mutators simply set/get a private member variable then they don't add any functionality. And they might add a bunch of unnecessary code if the optimizer doesn't take care of it.

In my opinion, accessors and mutators are grossly overused. If your class is used by one or to client applications that are developed in-house and all they do is set/get private member data then I think they shouldn't be used. In this case, you are trading certain complexity for an uncertain future benefit (that you'll need to run some code when setting/getting those members some day). If the code is all in-house then you can just modify the client code if you ever change the class.

So the bottom line is that they certainly have a use, but they come at a cost. Use accessors/mutators when

  • You are sure you need to run some code when setting/getting member data, or
  • there's a reasonable chance that you'll need to run some code in a future release and changing the client code will be difficult or impossible.
User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

AnswerBot

3d ago

Accessors provide read-only access to an object's data, ensuring data encapsulation and preventing unintended modifications. However, they cannot modify the object's data. Mutators, on the other hand, allow controlled modification of an object's data, but can potentially expose the data to unintended changes if not implemented carefully.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are advantage and disadvantages of accessors and mutator method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are accessors and mutator method in java?

An accessor is a method that accesses the contents of an object but does not modify that object. In the simplest case, an accessor just returns the value of one of the fields. In general, an accessor performs some computation using the fields as long as that computation does not modify any of the fields. Mutators A mutator is a method that can modify an object. In the simplest case, a mutator just assigns a new value to one of the fields. In general, a mutator performs some computation and modifies any number of fields. Sometimes, accessors are called 'getter' methods and mutators are called 'setter' methods.


What is a mutator in java?

A Java method that changes the state of an object is called a mutator. Mutators typically do not return a result (are declared to return void), although some mutators both change state and return a result (e.g., nextToken in StringTokenizer). Mutators just change state, without looking at current values, are often call "setters", and their names often start with set. In computer science, a mutator method is a method used to control changes to a variable.


What are the advantages and disadvantages of the inductive method?

An advantage of the inductive method is that it looks at nature to provide information. A disadvantage is that is sometimes goes against human logic.


What is the advantage and disadvantage of discounted payback method?

we only know the disadvantages: The cash flows beyond the payback period are ignored..


Advantages and disadvantages of documentary credit as compared to other methods of trade finance?

disadvantage of documentary credit payment method of international trade advantage and disavantage of advance payment method of international trade advantage and disavantage of bills of collection payment method of international trade


What are the advantages and disadvantages of correlation method?

The advantage of the correlational research method is the ability to prove a positive or negative correlation between two subjects . The disadvantage of this is the unclear interpreation of cause and affect. moletsane


What are the advantages and disadvantages of Registration Method?

Advantage is that people will be see you as you become the member and anybody can access you anywhere in the world and the disadvantage is that people can hack your info.


What are the disadvantages and disadvantages of least square method?

The disadvantages are that the calculations required are not simple and that the method assumes that the same linear relationship is applicable across the whole data range. And these are the disadvantages of the least squares method.


Advantages and disadvantages of Lowry's method?

Lowry's method tests for the total amount of protein in a solution. An advantage is that it is quick and easy to do with the color changes being readily recognizable. A disadvantage is that cysteine residue can create errors in the test.


What are the advantages and disadvantages of historical method of research?

An advantage is that it enables researchers to learn about events that happened in the passed or long ago and also provides a way to study trends


Disadvantages using cyanmethemoglobin method?

three disadvantages cyanmethemoglobin


What are the advantages and disadvantages of Delphi method in forecasting demand for a product?

advantages and disadvantages of delphi method of group technique