extends of the class with private constructor

There is no way out. You have to create an available (protected, public or default) super constructor to be able to extend test.

This kind of notation is usually used in utility classes or singletons, where you don’t want the user to create himself an instance of your class, either by extending it and instanciating the subclass, or by simply calling a constructor of your class.

When you have a class with only private constructors, you can also change the class to final because it can’t be extended at all.


Another solution would be having a method in test which create instances of test and delegate every method call from One to a test instance. This way you don’t have to extend test.

class Test {
    private Test() {
        System.out.println("test");
    }
    public static Test getInstance(){
        return new Test();
    }
    public void methodA(){
        //Some kind of implementation
    }
}

public class One {
    private final Test test;
    One() {
        System.out.println("One");
        test = Test.getInstance();
    }

    public void methodA(){
        test.methodA();
    }

    public static void main(String args[]) {
        new One();
    }
}

Leave a Comment