Home > Software design >  Data privacy and Access Modifiers java
Data privacy and Access Modifiers java

Time:10-08

I am currently in the middle of a project, and I have a slight dilemma. Say I have a class called A. In class A, there is a private nested class called B. Then I created another class called C which inherits A. Do I need to make B protected so that C can access it? or is there another way to keep B private and still let C access that class.

In general, is it better to use protected or use private and create getters and setters? Which one is better for variable / method privacy>

CodePudding user response:

No , that’s why private is for . You don’t want even class that inherit from you are able to access to it.

You can have a protected method which always return a new instance of B or return singleton of B, if you really need to access from your class child.

And for your last question , are you asking about variable inside classes?

  • Related