Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top