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

Viewed 31247

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

public final class ClassA{
  private ClassA{
  }

  public static void main(String[] arg) }{
  ;
  ;
  ;
  }
}

Now, i am extending the class 'ClassA' [ final keyword is removed before doing this ]

public class ClassB extends ClassA{
     public static void main(String[] arg) }{
      ;
      ;
      ;
      }

}

Now, i am getting Implicit super constructor classA() is not visible. Must explicitly invoke another constructor. What does it mean and how to resolve this?

Note i can not change the access specifier of ClassA constructor.

5 Answers

Because the son must invoke father's constructor for Complete the initialization of the parent's parameters.

Now ClassA is final class and it's constructor is private.If ClassB extends ClassA.

  1. remove final of the ClassA
  2. invoke super() or super(**), change private to public/protected, or add constructor for ClassA
public class ClassA {
    private ClassA() {
    }

    // add
    protected ClassA(String str) {
    }

    public void display() {
        System.out.println("Father's display");
    }
}
class ClassB extends ClassA {
    public ClassB() {
        // add
        super(null);
    }

    public void display() {
        System.out.println("Son's display");
    }
}
Related