final all methods and final class vs. final only class

I've got a final class where all methods have to be declared final. Now I read that the final declaration of the methods is obsolete.

Obviously the methods can't be overridden, but isn't there a difference betwenn final declaration of the class plus their methods on the on hand and only declare the class final on the other hand?

Differences in Performance, Subclassing or anything else?

Thanks

Martin

[437 byte] By [MartinBoehma] at [2007-11-27 5:52:26]
# 1

> I've got a final class where all methods have to be declared final.

Why?

> Now I read that the final declaration of the methods is obsolete.

It most certainly isn't obsolete, but it's redundant in this case.

> Differences in Performance, Subclassing or anything else?

Declaring the class 'final' prevents it being subclassed, which includes overriding the methods. Just declaring the methods 'final' doesn't prevent the class being subclassed but it does prevent the methods being overridden.

ejpa at 2007-7-12 15:43:27 > top of Java-index,Core,Core APIs...
# 2

> > I've got a final class where all methods have to be

> declared final.

>

> Why?

At first nobody should override the methods.

Now I'm not sure, if there are any impacts if I remove the final declaration of the methods. The origin of my question results out of some design and coding issues.

> > Now I read that the final declaration of the

> methods is obsolete.

>

> It most certainly isn't obsolete, but it's redundant

> in this case.

That's my question! In which cases it's obsolete?

Where is the difference between

1. final methods and final class

public final class myClass {

public final doSomething() {

}

protected final doIt() {

}

}

2. only final class

public final class myClass {

public doSomething() {

}

protected doIt() {

}

}

In which cases should I use which case? I don't suppose thats the same.

Thanks for your help

MartinBoehma at 2007-7-12 15:43:27 > top of Java-index,Core,Core APIs...
# 3

> > > I've got a final class where all methods have to

> be

> > declared final.

> >

> > Why?

>

> At first nobody should override the methods.

>

> Now I'm not sure, if there are any impacts if I

> remove the final declaration of the methods. The

> origin of my question results out of some design and

> coding issues.

If the class itself is final, making the methods final will have no effect whatsoever

> > > Now I read that the final declaration of the

> > methods is obsolete.

> >

> > It most certainly isn't obsolete, but it's

> redundant

> > in this case.

>

> That's my question! In which cases it's obsolete?

> Where is the difference between

> 1. final methods and final class

> > public final class myClass {

>

>public final doSomething() {

> }

>

>protected final doIt() {

> }

> }

>

>

> 2. only final class

> > public final class myClass {

>

>public doSomething() {

> }

>

>protected doIt() {

> }

> }

>

> In which cases should I use which case? I don't

> suppose thats the same.

>

> Thanks for your help

If you don't want your class to be subclassed at all, make it final. If you want to allow subclasses, but some of the methods should not be overriden, make only those methods final

georgemca at 2007-7-12 15:43:27 > top of Java-index,Core,Core APIs...
# 4
> That's my question! In which cases it's obsolete?It is never obsolete. You are misusing this word. It is redundant to declare methods final if and only if the class is declared final.
ejpa at 2007-7-12 15:43:27 > top of Java-index,Core,Core APIs...
# 5
Thanks for your answers.To sum up:Don't use the final keyword for methods, if the class is declared final, because of the redundant usage.
MartinBoehma at 2007-7-12 15:43:27 > top of Java-index,Core,Core APIs...
# 6
I wouldn't put it as strong as 'don't'. I'd just say it's redundant. No harm is done.
ejpa at 2007-7-12 15:43:27 > top of Java-index,Core,Core APIs...