Is there any way to access a private inner class that is nested in a super class from a derived class in Java?

StackOverflow https://stackoverflow.com/questions/22258662

  •  11-06-2023
  •  | 
  •  

Question

Maybe I did not phrase the question correctly. Here is the code I have as an example:

//a generic binary tree class
public class BinaryTree<T extends Comparable<T>> {

  //inner node class
  private final static class Node<T>{

    //code
  }

   private Node<T> root; //instance variable for this class


   //methods
        //assume setters/getters for the root
}

//MyTree class. I am extending it because I want to use all of the
//methods in the BinaryTree class and then some more methods that apply
//only to this class

 class MyTree extends BinaryTree<Student> {

       void aMethod(){

             //I want to be able to do this but I get errors about trying to access a private class
             Node<Student> node = root;

       }

   public static void main(String[] args) {


   }

  }

  class Student implements Comparable<Student> { //implemented method here }

In the above example I want to be able to access the root in MyTree class. How can I do this? Maybe there is a better way to set this up? I am thinking that it may be best to take out the inner Node class and make that its own class?

Was it helpful?

Solution

When you make a nested class private, it means that the class is part of the implementation detail of your main class. That is, your main class has the public methods that it needs to provide, and in order to provide those, the programmer has decided to define another class to help with the implementation (for example, to hold some kind of table of data that the public methods use). (Sometimes this means that the class is getting too big and complicated, and that some refactoring is in order. But that's a matter for another question.)

In any case, if you want classes that use BinaryTree<T> to be able to use Node<T>, that's an indication that Node<T> is an important part of the purpose of the BinaryTree class (as it appears to clients), and not merely an implementation detail. That means it probably shouldn't be private.

OTHER TIPS

//inner node class
private final static class Node<T>{
   //code
}

public Node<T> root; //instance variable for this class

root is not a member of the inner class, and it is public, so you already have access to it from any class, not just sub-classes of BinaryTree. Are you sure that variable should be public and mutable?

But to answer your question, the inner class Node must be public or protected in order for it to be accessible from the sub-class. Changing it to any of the following will work. Likely, the protected is the best choice.

public final static class Node<T>{
protected final static class Node<T>{
final static class Node<T>{

Note the third option is only for when the sub-class is in the same package as the super-class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top