tags:

views:

387

answers:

6

Is there any particular reason I should declare a class or a method final in day-to-day programming (web or otherwise)? Please provide a real-world example where it should be used.

BTW, I'm asking because I'm trying to pick an 'obscure' keyword and master it.

+7  A: 

It prevents other programmers from doing stuff with your classes that you don't intend for them to do. So rather than making a comment saying "don't use this class to do XXX", you can design it in such a way that they won't be tempted to override it and abuse it in that manner.

Edit: Since an example is requested, I'll describe a case where you might find this keyword handy... say you have a class which defines how two objects may communicate with each other, say, a listener and notifier. The listener class must obviously allow inheritance, but you might want to make the notifier class final so that it must be used in a "has-a" relationship. This way, you can make some assumptions from your listener objects about the nature of the messages they are receiving.

Otherwise, it would be possible to inherit from that class, do all sorts of crazy stuff like extend the messages and so on. If you don't want other programmers to do that, then you could make the class final. Also, this might make a lot more sense in a widely-used public API, as it also allows other programmers to readily understand which classes are ok to subclass.

Another example -- say you have a buffer-based stream processor, and inside of your read()/write() method, you maintain some data about the current state of your object (ie, the current byte or whatever). There is no way to guarantee that anyone subclassing this class would call the super's methods during processing -- and since such a class likely only contains a few methods, it's better to just make the whole thing final instead of each method. This again forces people using the class to "has-a", not "is-a", and thus, can control how you expect the code will be executed.

I would agree with you in that making an entire class final is probably something which you'd need to do once in a blue moon, but it's nice that PHP has this capability should you choose to exercise it.

Nik Reiman
I think a comment would be very appropriate this way or the other.
Pies
Can you provide a real-world example where you'd use it that way?
Pies
Yes, you can comment it too, but it's better to assume that people who work with your code may not read the documentation or follow it very carefully. Imposing a restriction through design is much more powerful.
Nik Reiman
Can you give an example?
Pies
+3  A: 

Some people claim that you should declare all classes final unless you specifically intend to allow, and have examined the implications of, polymorphic extension of it. I tend to feel this implies a lot of distrust of whoever else is working with your code, but unfortunately, sometimes that's warranted.

chaos
Initially I was going to ask "Should every class be declared either final or abstract?"
Pies
I'd definitely say no to that one. :)
chaos
Can you make an argument against it?
Pies
Sure. Let's say your DAL or ORM has a Row class that the application developer *can* extend to layer on table-specific functionality, but *need* not -- Row by itself will work fine for general purposes. Declaring it abstract and forcing subclassing is just annoying and pedantic.
chaos
A: 

A final class is a class that can not be subclassed, so anything that you don't want to have derivatives created of. For instance in Java, the Math class is final since Java doesn't want you to be able to redefine what Absolute value means.

jtyost2
PHP classes are immutable.
Pies
And if I did create a subclass of the Math class, I sure would like to be able to redefine what Absolute value means. Since subclassing implies you want to modify how the class works, "final" prevents subclassing, not modification as such. Why would you want to prevent that?
Pies
@Pies - well, with the given example, Absolute value has one meaning and it should not be changed, even if you want it to.
mabwi
What you're saying is that "final" means that the particular class makes no sense to extend?
Pies
@Pies Yeah, exactly.
jtyost2
Actually, Math is final because it's a static utility class - it has no instance methods or properties. It's common to make such a class final to lock it down.
Software Monkey
It also enforces using Math.xxx, instead of math=new Math ... math.xxx
Software Monkey
A: 

final (and sealed in c#) could be used (by class library designers) to enforce some basic behaviour of a class.
For instance enforce that the string has immutable behaviour
The string class in java is marked final and in c# it is marked sealed.
All strings are immutable and that behaviour cannot be changed.

Kb
But couldn't one extend that argument to all classes except abstract ones? In other words, doesn't that mean that all classes should be either abstract or final?
Pies
@Pies: I would not argue for that statement. Anyway interesting question. ;))
Kb
+5  A: 

"Enforce Composition over Inheritance" puts it rather succinctly. You guarantee a certain behavior of your class that nothing else can interfere with, which, as the next section explains, can have security benefits as well.

Sören Kuklau
But PHP classes are immutable, so that doesn't apply.
Pies
+2  A: 

In many languages, declaring a class as final also provides optimization benefits, since it needn't look at the virt table for methods.

If you want to enforce the intent of the class, and the intent is such that it doesn't make sense to subclass, then use final; otherwise, there's no significant advantage.

In general, it's just a mechanism for enforcing intent.

Ryan Emerle
Can you give an example?
Pies