+ 3
Can anyone explain how set() and get() methods are related to abstraction?
4 Antworten
+ 2
What is the function of get() and set() methods in Java?
+ 1
give access to private members of a class
+ 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;
}
}
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.