How can I prevent inheritance without marking a class final?

Could u pls help on this
[31 byte] By [RamcharanTeja] at [2007-11-27 6:49:21]
# 1
Why can't you use the final keyword? You can't prevent inheritance without it.
kevjavaa at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 2
I don't want to do that.I want to know other than that
RamcharanTeja at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 3
By declaring the base class members(properties and methods) as private, you can resist that class from inheritance
spsuryaina at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 4
> I don't want to do that.I want to know other than thatWell, 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?
kevjavaa at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 5
> How can I prevent inheritance without marking a class final?I do not see the point of the question but mark every method final not the class.
BIJ001a at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 6
> By declaring the base class members(properties and> methods) as private, you can resist that class from> inheritanceSo you prevent inheritance of those specific individual members, not of the class.
kevjavaa at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 7
This is to prevent the inheriting of members.
RamcharanTeja at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 8
I believe thats the only way to achieve his concept. It allows to inherit. But wont give access to them
spsuryaina at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 9
> I don't want to do thatThe question asked was "why not?".
karma-9a at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 10
> 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.
prometheuzza at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 11

> > 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 :).

kevjavaa at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 12
Only provide private constructors and some non-private static methods to create instances.-Puce
Pucea at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 13
Making it a pain in the ass 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.
kevjavaa at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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

Pucea at 2007-7-12 18:23:01 > top of Java-index,Java Essentials,Java Programming...
# 15

> 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*

kevjavaa at 2007-7-21 22:06:53 > top of Java-index,Java Essentials,Java Programming...
# 16

> > 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.

kdgregorya at 2007-7-21 22:06:53 > top of Java-index,Java Essentials,Java Programming...
# 17
> No, you CANNOT extend a class, which only provides> private constructors!> > -PuceThat was not completly true. You CAN call a private super constructor but only from nested classes. :-) No one can call it from another Java file though!-Puce
Pucea at 2007-7-21 22:06:53 > top of Java-index,Java Essentials,Java Programming...
# 18

> > > 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...

jwentinga at 2007-7-21 22:06:53 > top of Java-index,Java Essentials,Java Programming...
# 19

> 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?

karma-9a at 2007-7-21 22:06:53 > top of Java-index,Java Essentials,Java Programming...
# 20

> > 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

Pucea at 2007-7-21 22:06:53 > top of Java-index,Java Essentials,Java Programming...
# 21

> > > 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.

karma-9a at 2007-7-21 22:06:53 > top of Java-index,Java Essentials,Java Programming...
# 22
> if you only have static methods.> No instance will ever be created...You can still create instances WITH static methods.-Puce
Pucea at 2007-7-21 22:06:53 > top of Java-index,Java Essentials,Java Programming...
# 23
> And I'm still waiting for his response.Given the nature of his question and his responses, my guess is that this is all way over the OP's head.
petes1234a at 2007-7-21 22:06:53 > top of Java-index,Java Essentials,Java Programming...