Can anyone explain how set() and get() methods are related to abstraction? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can anyone explain how set() and get() methods are related to abstraction?

8th Nov 2016, 2:49 PM
Jenny Lance
Jenny Lance - avatar
4 Answers
+ 2
What is the function of get() and set() methods in Java?
8th Nov 2016, 2:54 PM
Jenny Lance
Jenny Lance - avatar
+ 1
give access to private members of a class
8th Nov 2016, 2:58 PM
Laster Maluleke
Laster Maluleke - avatar
+ 1
Basic good coding practice is to only allow public access to the methods and variables you want the outside to manipulate and nothing is left exposed. Set your classes variables to private so that when they are needed you can write a public method that sets a valid value and one that gets the current value of those variables that are private. EXAMPLE: public class example{ private int x; public void setValueOfX(int valuePassed){ x = valuePassed; } public int getValueOfX(){ return x; } }
11th Nov 2016, 9:49 PM
Sean Corcoran
0
Hello! I think methods get() and set() are more related with the concept of Encapsulation than Abstraction. The set() method will allow you to insert data and the get() method will allow you to recover or read some data, those are usefull because with them you can control de way that a user or other program introduces or gets information of your program. In other words, it a good habit, and necessary, to use the get() and set() methods as "walkways" of the information to your programs. I hope my answer will be usefull.
14th Dec 2016, 7:00 PM
Matias Ojeda
Matias Ojeda - avatar