1

I have recently joined a new company and I am trying to get used to their coding style guidelines. I have no problem changing my coding style, but one particular point, I am not sure whether they are right or not.

For my first task I had to extend one of the existing abstract classes to develop a particular functionality. Thus I needed to access many attributes declared in this abstract superclass. To do so I proposed to change the visibility of these attributes and declare them as protected. My surprise came with their reply:

"Never! That is absolutely against OOP and you would produce obscure and difficult to maintain code! What you have to do is creating a getter in the super class and using it from the subclass in order to access these attributes".

Well, I have been always using protected attributes in an abstract superclass and accessing them from the subclass directly and I always thought there was nothing wrong with it. Even I would say that calling all the time the getter to access an attributes in the super class is slower than using it by its name...

What do you think about it? Is it normal/standard coding style declaring the attributes in a superclass and accessing them directly or are you of the oppinion that is better creating getters for these attributes.

To sumarize, my way:

public abstract class A {

    protected String variableA="a";

    public abstract methodToImplement();    

}

public MyClass B extends A {

    public methodToImplement() {
        System.out.println(variableA.length());
    }

}

Their way:

abstract class A {
    protected String variableA="a";
    public String getVariableA() {
        return variableA;
    }
    public abstract methodToImplement();
}

MyClass B extends A {
    public methodToImplement() {
        System.out.println(getVariableA().length());
    }
}

Thanks.

daniel sp
  • 937
  • 1
  • 11
  • 29
  • The point of a protected variable is that is is accesible by subclasses. Writing a getter/setter is not really necessary in this case. So in my opinion, I would prefer accessing them directly. It's simply put less code, and you know where the variable is coming from. A getter and setter can be useful if you need extra constraints on your variable. – Christophe De Troyer Apr 03 '14 at 08:13
  • possible duplicate of [Java protected fields vs public getters](http://stackoverflow.com/questions/2279662/java-protected-fields-vs-public-getters) – JonK Apr 03 '14 at 08:14
  • And this: http://stackoverflow.com/questions/19666652/changing-the-value-of-superclass-instance-variables-from-a-subclass – Christophe De Troyer Apr 03 '14 at 08:14
  • And this: http://stackoverflow.com/questions/3525765/protected-data-in-abstract-class – Andrew Martin Apr 03 '14 at 08:15
  • I am afraid this is a question that is not really suited for Stack Overflow, as you will probably yield just opinions, not a "right" or "wrong". Also I suppose "Their way" should declare `variableA` as `private` as otherwise there would be no point in using the public getter and you would not need to change the variables visibility in the first place. As you can see from the other comments Java programmers usually prefer to have variables private, as I also write Ruby code (where there is only limited privacy for variables of superclasses) I do not have problems with using protected variables. – Patru Apr 03 '14 at 08:18

1 Answers1

1

So as other threads already point out it appears to be so that it's indeed recommended to use getters and setters. The reason being that if you ever plan to change the representation of that value (StringBuilder instead of String for example) you will have to change your code. A getter/setter allow you to program in a way that you send the getters/setters the data you want, and they will store it in the proper field for you (e.g., appending it to the StringBuilder). So yes, it apears to have a lot of advantages, even though it's not your coding style. However, declaring the variable as protected seems pretty weird when you use a getter and a setter as well..

I personally try to avoid getters/setters when they are a bit of overkill. To me they are overkill for value variables. For reference variables they are however a good idea.

However, I think there is no right or wrong here..

Christophe De Troyer
  • 2,852
  • 3
  • 30
  • 47