简体   繁体   中英

Accessing methods/fields in public class from private class

Is it bad practice or "dumb" to access methods/fields that are private in the public class from private classes in the same file. In my case I have a method that add components in my GUI to panels(GridBagLayout) so I have made a method for this. However I have three panels so instead of making a addComponent-method in each private class I have the private method addComponent in the public class.

This is a overview of my class:

  • RegisterQuestionGUI (public)
    • This class has many methods, one of them is a private method named addComponent.
    • I also have three private classes that extend JPanel, and all of these classes use the addComponent in exactly the same way.

So back to my question, is this a good/bad way of doing it?

Thanks in advance.

A private inner class is part of the public "outer" class. Therefore, accessing the private members of the outer class is perfectly acceptable.

In general, I don't see an issue with it. Private inner classes are part of the implementation of the outer class, so encapsulation is not broken. OTOH getting rid of duplication is a good thing.

AFAIK this idiom is used many times in the class library (it is there for a reason, after all :-), eg when implementing Iterator s in the Collection Framework. Its typical usage tends to have the following common traits:

  • you need to implement a specific interface without publishing the concrete implementation class, however
  • the implementation is tightly bound to some public class (making the two in fact a component ).

Implementing the interface in a private inner class nicely satisfies both constraints at once, making the logical codependency of the two classes explicit, and encapsulating the implementation class.

It is excellent.

You need private classes because (I guess) you have to implement certain interfaces (ie EventListener etc). You make them inner classes because they are irrelevant beyond outer class. But you re-use the code creating private utility in outer class. So, you are a good programmer.

It depends.

If the private classes are trivial helpers, it can be reasonable to think of them as part of the implementation of the main class.

However, you might want the private class to access only non-private methods of its containing class if:

  • It is a non-trivial nested class.
  • If you might someday want to move the nested class, for example, to become a top-level class.

I'd say that was perfectly acceptable - I've done similar things in the past. Accessing private variables from an inner class is allowed for a reason, in many situations (not just this one) it makes sense to do so.

In general this is fine. However, depending on what these JPanel classes are, it might make more sense to break them out a separate classes. Maybe even have them implement the same interface so that your RegisterQuestionGUI can interact with them the same way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM