How to resolve 'Implicit super constructor classA() is not visible. Must explicitly invoke another constructor'?

前端 未结 4 1389
[愿得一人]
[愿得一人] 2020-12-17 17:12

I am having a class \'ClassA\' which is having private constructor.

public final class ClassA{
  private ClassA{
  }

  public static void main(String[] arg)         


        
4条回答
  •  独厮守ぢ
    2020-12-17 17:18

    I would suggest composition instead of inheritance (maybe that's what the designer of ClassA intended for class usage. Example:

    public class ClassB {
       private ClassA classA;
    
       ClassB() {
           // init classA
           ...
       }
    
       public ClassA asClassA() {
           return classA;
       }
    
       // other methods and members for ClassB extension
    }
    

    You can delegate methods from ClassB to ClassA or override them.

提交回复
热议问题