Why use getters and setters in classes?

Asked

Viewed 1,214 times

0

Because I need to encapsulate the entire class, if, as a programmer, I know perfectly how to use that variable. I only see need for setters and getters that work the variable value, so:

void setCommand (string val)
{
    string ret = "";

    for (char it : val)
        ret += toupper(it);
    command = ret;
}

But what is the need to use setters or getters like this:

void setMoney (float val)
{
    money = val;
}
float getMoney ()
{
    return money;
}
  • Also: https://answall.com/q/44739/101 and https://answall.com/q/40416/101 and https://answall.com/q/212300/101 and https://answall.com/q/129353/101 and https://pt.stackoverflowcom/q/204355/101 and https:///pt.stackoverflowcom/q/45285/101 and https://en.stackoverflow.com/q/133924/101 and https://answall.com/q/202227/101 and has a number of members who I will not post here.

  • There is only one that already answer this. I didn’t even need a survey, at the time I was typing appeared all of them.

1 answer

1

Basically this is one of the concepts of object orientation, encapsulation, in which you can define your variable as private, and create the getter and Setter to be able to access it from another class

ex:

class Pessoa {
  String nome;
  public String getNome() {
    return this.nome;
  }
  public void setNome(String s) {
    this.nome = s;
  } 
}

Main class

class main() {
  public static void main(String[] args) {
    Pessoa pessoa = new Pessoa();
    pessoa.setNome("João");
    System.out.println(pessoa.getNome());
  }
}

In this situation I created the class Person and set within it a private variable of the type String called name, in which I want to access it outside the class Person, for this I created the Setter and getter var person, making it possible to access it in other classes

In the former I gave, I transferred it from the main class, created an object of the type Person, and set a name to it "John" from the method setNome() of the class Person, dps displayed a name that I passed the same on the screen through the method getNome()

  • 1

    Actually this is a common mistake. There’s nothing in OOP that says they should exist getters and setters. Encapsulation is something broader and more conceptual than this (https://answall.com/q/96656/101). And the question was about why and not how it works since the question implies that this is already known. Most important of all is that most people create this pair of methods thinking they’re doing OOP when in fact OOP has opposite philosophy. is to create methods that make something useful and not just a bulkhead for the fields.

  • 1

    It’s like I said the other day, OOP is like teenage sex, everyone talks about it, but do even few do. First you need to understand what OOP is to be able to do it right. Most people think they know because they follow the cake recipes around. There are reasons to use these methods, but conceptually OOP preaches something more abstract in encapsulation, getters and *setters are too concrete. And often it’s to break all this.

  • 1

    From what my teacher taught me, the Setter and Getter method are used so that we do not have direct access to the variable, thus causing damage to the system when n accessed correctly, by ourselves, or by other programmers

  • 1

    This is what I said, people follow rules blindly without understanding the real reason of the mechanism. And it does not cause damage to any system. That is, without understanding the real reason they exist no one will use right. Without using right is better not to use. And because they learned wrong, they teach wrong and then it creates a vicious circle where everyone thinks they learned right. It’s the same with comments where almost all teachers talk about using and abusing them, when it’s the other way around. It made sense in the '50s/60s, it was in the books, it stopped making sense, but they keep teaching it.

  • 1

    All this because people learn the rules and not why they exist. The question, although duplicate, is very good because it asks the Why, which is the best one should learn. This is how it is understood when to use or not. I’m a huge critic of good practice, because people’s minds are cracked. They teach and rule and not the motivator of the rule (except exceptions, but there no longer need to call it good practice), and people know the mechanism and not when to use or not to use, how to apply. Search there: https://answall.com/search?q=user%3A101+boa+pr%C3%A1tica

Browser other questions tagged

You are not signed in. Login or sign up in order to post.