In my data structures class, we are implementing the Node
s of a red/black tree using a private inner class. This kind of Node
is unique to red/black trees, so the inner class's visibility is private
. The members of an inner class are always accessible from the enclosing class.
How should I choose a visibility for the members of this inner class? If there is no difference to the compiler, which visibility would make the most sense semantically?
public class RedBlackTree<K extends Comparable<? super K>, V> {
private static enum NodeColor { RED, BLACK }
private class Node {
private final K key; // these are private, currently for no particular reason
private V value;
private Node left;
private Node right;
private NodeColor color;
private Node(K key, V value, NodeColor color) {
this.key = key;
this.value = value;
this.color = color;
}
}
// Precondition - both children are red, and node is black.
// Postcondition - both children are black, and node is red.
private void colorFlip(Node node) {
node.color = NodeColor.RED;
node.left.color = NodeColor.BLACK; // these statements compile, as expected
node.right.color = NodeColor.BLACK;
}
}
In this question, the OP said private
made sense semantically, while in this answer, the author said public
made sense semantically. Which one is best practice, if any?