> > Well, I want to jump out of a plane, but I don't want
> > to wear a parachute. Is there any way you can help
> > me to not go splat?
>
> Jump out of a (small) plane which is still on the
> ground.
Indeed, I'll have to spend a little more time on my next analogy :).
> Making it a pain in the *** to inherit from your
> class doesn't equate to preventing inheritance.
>
> You can still override a private constructor, you
> just can't call it from the derived class.
How do you want to override ANY constructor? :-) Constructors are not overriden just called!
No, you CANNOT extend a class, which only provides private constructors!
-Puce
> How do you want to override ANY constructor? :-)
> Constructors are not overriden just called!
>
> No, you CANNOT extend a class, which only provides
> private constructors!
>
> -Puce
You are absolutely correct, and I offer my apologies. You cannot call a private method in a base class, period, so the implicit super constructor call results in a compile-time error.
*brainfart - need more coffee*
> > Well, I want to jump out of a plane, but I don't want
> > to wear a parachute. Is there any way you can help
> > me to not go splat?
>
> Jump out of a (small) plane which is still on the
> ground.
Hah. You've never tried jumping out of a small plane then. I get worried about going "splat" just climbing into one. Big cargo doors, that's what we need!
To the OP: was this an interview question? Here's an answer: make all your constructors private (you need to have at least one, and it has to be private).
To other posters: marking methods as final won't prevent inheritance. It just means that you can't override those methods.
Edit: looks like puce already gave the answer. Memo to self: read thread before trying to answer one post.
> > > Well, I want to jump out of a plane, but I don't
> want
> > > to wear a parachute. Is there any way you can
> help
> > > me to not go splat?
> >
> > Jump out of a (small) plane which is still on the
> > ground.
>
> Hah. You've never tried jumping out of a small plane
> then. I get worried about going "splat" just climbing
> into one. Big cargo doors, that's what we need!
>
paramodified Cessna 206 maybe?
Jumping from your desk will be almost more dangerous.
> To other posters: marking methods as final won't
> prevent inheritance. It just means that you can't
> override those methods.
>
marking classes as final will ;)
And having private constructors doesn't matter much if you only have static methods.
No instance will ever be created...
> Only provide private constructors and some
> non-private static methods to create instances.
This will work. However, using final is a better solution and declares more clearly the intent. After all, that was what it was meant for when applied to a class.
By using a private constructor, you mean to say that the class should not be instantiated outside of itself, not that the class should not be sub-classed, i.e. the impossibility to subclass is a side-effect, not the intent.
Then, as you stated yourself, you need to provide static factory methods for the sole reason to be able to create instances outside the class, as a result of the limitations introduced with private Ctors.
This may look purely academic, but it really isn't.
Because you'll have to explain all that while documenting your code, i.e. why you did choose a less obvious and more complex solution.
The fact remains that using final is the simplest and best solution to what the OP wants to prevent. So I guess I'll have to ask the OP again:
why can't you use final?
> > Only provide private constructors and some
> > non-private static methods to create instances.
>
> This will work. However, using final is a better
> solution and declares more clearly the intent.
> After all, that was what it was meant for when
> applied to a class.
>
I did not say it is better, it is just another solution to the problem. :-)
There are pros and cons for factory methods.
-Puce
> > > Only provide private constructors and some
> > > non-private static methods to create instances.
> >
> > This will work. However, using final is a better
> > solution and declares more clearly the
> intent.
> > After all, that was what it was meant for when
> > applied to a class.
> >
>
> I did not say it is better,
I know you didn't.
Although I quoted your response, my comment was meant more for the OP to chew on.
And I'm still waiting for his response.