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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…