Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are protected variables not allowed by default in Checkstyle?

I get a lot of warnings in eclipse like these:

Variable 'myVariable' must be private and have accessor methods.

I think I get them because I didn't set protectedAllowed manually to true in eclipse. But why is it set to false by default? Shouldn't I use protected attributes?

like image 744
Martin Thoma Avatar asked Dec 11 '11 16:12

Martin Thoma


People also ask

Why are protected variables bad?

Putting protected on a member variable breaks encapsulation because now a derived class has access to the implementation details of the base class. It's the same problem that occurs when you make a variable public on an ordinary class.

Are protected variables bad?

Protected member variables have significant disadvantages because they effectively allow client code (the sub-class) access to the internal state of the base class class. This prevents the base class from effectively maintaining its invariants.

What is a protected variable in a class?

Protected variables are those data members of a class that can be accessed within the class and the classes derived from that class. In Python, there is no existence of “Public” instance variables. However, we use underscore '_' symbol to determine the access control of a data member in a class.

When should I use protected C++?

The protected keyword specifies access to class members in the member-list up to the next access specifier ( public or private ) or the end of the class definition. Class members declared as protected can be used only by the following: Member functions of the class that originally declared these members.


2 Answers

Theoretically, protected attributes (variables) are an anti-pattern in object-oriented languages. If only subclasses need to access member attributes of its superclass, define the attributes themselves as private and create protected accessor methods (getter and setter). This approach applies the concept of 'information hiding'. There is an alternative solution: define protected immutable (final) member attributes.

Further readings:

  • Should you ever use protected member variables?
  • http://www.codinghorror.com/blog/2006/08/properties-vs-public-variables.html
like image 159
home Avatar answered Sep 20 '22 08:09

home


I guess, making everything private is an anti-pattern. Often classes are used in a bunch and as a whole represent encapsulated entity placed in separate package. They do not need to hide something from each other, but this rule enforces hiding for no good reason, increasing clutter and effectively making style (as I understand it) worse. Meanwhile, we often see that every class in package is public. I guess this is much worse, but checkstyle doesn't check that.

Encapsulation exists not only on class level, put also on package, system and so on. And I think that these levels are even more important.

like image 30
Rorick Avatar answered Sep 18 '22 08:09

Rorick