简体   繁体   中英

In java, how do I make a class with a private constructor whose superclass also has a private constructor?

As an example:

public class Foo {
    private Foo() {}
}

public class Bar extends Foo {
    private Bar() {}

    static public doSomething() {
    }
}

That's a compilation error right there. A class needs to, at least, implicitly call its superclass's default constructor, which in this case is isn't visible in Foo .

Can I call Object 's constructor from Bar instead?

You can't. You need to make Foo's constructor package private at the very least (Though I'd probably just make it protected.

(Edit - Comments in this post make a good point)

This is actually a symptom of a bad form of inheritance, called implementation inheritance. Either the original class wasn't designed to be inherited, and thus chose to use a private constructor, or that the entire API is poorly designed.

The fix for this isn't to figure out a way to inherit, but to see if you can compose the object instead of inheriting, and do so via interfaces. Ie, class Foo is now interface Foo, with a FooImpl. Then interface bar can extend Foo, with a BarImpl, which has no relation to FooImpl.

Inside BarImpl, you could if you wish to do some code reuse, have a FooImpl inside as a member, but that's entirely up to the implementation, and will not be exposed.

You won't be able to create an instance of Bar for as long as Foo has a private constructor. The only way you would be able to do it is if Foo had a protected constructor.

You can't call Object's constructor directly from Bar while it's a subclass of Foo, it would have to through Foo's constructor, which is private in this case.

When you declare Foo's constructor private, it does not create a default public constructor. Since Bar has to invoke Foo's constructor, it is not possible to leave it private. I would suggest, as others have, on using protected instead of private.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM