Why make private inner class member public in Java?

前端 未结 7 2005
情深已故
情深已故 2020-12-02 15:54

What is the reason of declaring a member of a private inner class public in Java if it still can\'t be accessed outside of containing class? Or can it?

publi         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 16:19

    There are many combinations of access modifiers which are not useful. A public method in a private inner class is only useful if it implements a public method in a public class/interface.

    public class DataStructure {
        // ...
    
        private class InnerEvenIterator implements Iterator {
            // ...
    
            public boolean hasNext() { // Why public?
                // ...
            }
        }
    
        public Iterator iterator() {
            return new InnerEvenIterator();
        }
    }
    

    BTW: abstract classes often have public constructors when actually they are protected

提交回复
热议问题