Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 779
Rakesh Juyal Avatar asked Oct 11 '10 07:10

Rakesh Juyal


2 Answers

Change the constructor visibility of ClassA from private to protected.

Constructors always begin by calling a superclass constructor. If the constructor explicitly contains a call to a superclass constructor, that constructor is used. Otherwise the parameterless constructor is implied. If the no-argument constructor does not exist or is not visible to the subclass, you get a compile-time error.

like image 177
Julien Hoarau Avatar answered Oct 16 '22 02:10

Julien Hoarau


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.

like image 26
Bivas Avatar answered Oct 16 '22 02:10

Bivas