The confusion is when to use private protected fields

I have seen users in SO saying that protected fields are bad because it can cause problems as code grows. See the following code.

public class Car {
    private String modelName;
    private int yearReleased;

//getters and setters

}

If the Car class is extended by a class named ToyotaCar

public class ToyotaCar extends Car{
   // Toyota specific stuff
}

I want my ToyotaCar facility to have fields modelNameand yearReleased. That's why I decided to leave the Car class. But private members are not inherited by the subclass (although I could access these fields using the public getter and setter). Now, my confusion is whether I should make stickers in the Car class secure, not private. But people say that this creates problems.

Does this mean, regardless of which class you always write, make the fields private?

, protected? , ?

+4
3

: - 'private' . , , () . "", .

, (getters seters) .

+5

(- /) , protected.

, , , public/protected member , , public/protected member .

, , , .

+1

, , [], , .

, . , , , , , , - getter, .

, , Car , ToyotaCar .. , getter seters . , .

, .

-1

All Articles