Suppose there is a class with all of its constructors declared as private.
Eg.:
public class This {
private This () { }
public someMethod( ){
// something here
}
// some more-- no other constructors
}
From what I know, making all constructors private is similar to declaring the class "This" as final-- so that it can't be extended.
However, the Eclipse messages i'm getting are giving me the impression that this is possible-- an all-constructors-private class can be extended. Take a look at this:
When I attempt to extend this class with something like
public class That extends This {
...
}
Eclipse giving me an error that: "Implicit super constructor This() is not visible for default constructor. Must define an explicit constructor."
When i define a constructor of its own:
public class That extends This {
That () {..}
...
}
this time i'm getting : "Implicit super constructor This() is not visible for default constructor. Must explicitly invoke another constructor."
Is there a way to get around this-- of extending a class of which all constructors are private?
if yes, how?
if no, what's the difference between stopping a class from being extended by i.) making its constructors private, and ii.) defining it as final?
Note: i saw Can a constructor in Java be private? among some other discussions.
A class with private constructors cannot be instantiated except form inside that same class. This make it useless (possible, but will not compile) to extend it from antoher class.
This does not mean it cannot be subclassed at all, for example among inner classes you can extend and call the private constructor.