Skip to main content
2 of 2
edited title
Tlaloc-ES
  • 387
  • 1
  • 12

use always get and set methods is a bad practique, is call directly an attribute class a bad practique in OOP?

I have a doubt about if there are some recommendations for call directly an attribute in a class

I think that in OOP you always should call an attribute by the get method.

For example:

On set

public setAtribute(Atribute atr){ this.atr = atr;}

On get

 public Atribute getAtribute(){ return this.atr;}

On the constructor

public Constructor(Attribue atr){
    setAttribute(atr);
}

If the attribute is injected in the super class I use this way to call it, on inherits class:

@Inject
protected Attribute atr;

public Attribute getAttribute(Attribute atr){
    return atr;
}

I.E. I prefer to use always get and set in OOP to get access to the attributes.

But in my work there is a debate because there are people that not like this way. Because you can wrote the following code:

getAttribute().callSomeFunctionality();

But I prefer these instead of:

this.callSomeFunctionality();

And I think that this not break the Demeter´s Law because is a variable class.

What do you think about that?

Is the best use always the get and set function, or there are other cases to call directly to attribute.

There is some of the literature about this?

Thanks

Tlaloc-ES
  • 387
  • 1
  • 12