Confusion on when to use private vs protected fields

牧云@^-^@ 提交于 2019-12-07 07:35:12

问题


I have seen users in SO saying that protected fields are bad, because it can introduce problems as the code grows. Please refer to 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 object to have a modelName and yearReleased fields. And that is why I decided to extend from Car class. But private members are not inherited by the subclass (even though I could access those fields using a public getter and setter). Now my confusion is whether I should make the fileds in the Car class to protected instead of private. But people say that introduces problems.

Does it mean no matter what class you write always, make the fields private?

If so on what instances the protected keyword is used? is it only for methods which we are planning to use in our subclasses?


回答1:


You nailed it yourself: a good practice is to make everything 'private' by default. Then, your specific design may require for example to be able to use some attributes or (preferably) some methods inside a subclass. In that situation, you'll need to move them toward 'protected' - but only in that situation.

Remember that using the accessors (getters & setters) is perfectly ok, and can be done without breaking encapsulation.




回答2:


If there is a strict urgency(due to a specific design/pattern) of changing the fields from the subclass, then you should go declaring your class fields as protected.

If not so, then generally the better approach is to perform the same using a public/protected member method in the parent class updating those private fields in the parent class and then, calling that public/protected member method from your child class' object.

This way you can achieve the implementation by calling parent's class member method from the child class' object to update those parent class' private fields.




回答3:


Protected keyword for declaring the variables is used to make those instance variables visible for all the other classes in the same package and also the class[sub class] which will extends the super class involving those protected variables.

of course,you can declare the variables with private or protected modifiers.But when you declare the variable as private then you can able to hide variable such that other classes are not able to access it directly and on the other hand if you declare the variable with the protected then you are making the variable to access it directly without using any getter methods,which is against to OOP principle.

So from my opinion, Since Car is the super class of all the other class like ToyotaCar and so on.Declare the variables in your super class as private and in sub class make use of getter and setters methods to read and write depending upon your need. By doing that you are adhere to OOP principles.

Hope this helps.

Thanks



来源:https://stackoverflow.com/questions/29576685/confusion-on-when-to-use-private-vs-protected-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!