The correct would be to leave the attributes of the abstract class as private or protected?

10

I do not know how I should leave those attributes.

    
asked by anonymous 13.02.2016 / 18:24

1 answer

9

It depends on what you want. If you leave it private, the derived class will not be able to access it directly. It's usually what you want. If the derivative needs to access the attribute directly for something, leave it protected. Indirectly (through a protected or public method) it is clear that it can be accessed.

Some people say that protected should not even exist in the language. I think it's an exaggeration, but gives an indication that it should be used sparingly. Many think that either the thing is public or private. I'd rather have more granularity. Only experience will indicate when something really should be protected without causing problems. So some prefer never to have protected attributes. Private attribute always gives more freedom to those who draw the abstract (or normal) class. The private is implementation detail. The protected already creates a coupling that may be unnecessary or even harmful in some cases.

I'm not a fan of the term attribute for this, I prefer field .

    
13.02.2016 / 18:30