Timeline for Why is using 'final' on a class really so bad?
Current License: CC BY-SA 3.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 12, 2022 at 15:35 | comment | added | gnasher729 | Do not mark a class as non-final unless you have spent the effort needed to make it subclassable. The meaning is "you can't subclass it unless you remove the 'final' keyword". It's not "you can't subclass it, and someone will cut your throat is you try". | |
Jul 2, 2011 at 14:13 | comment | added | S.Lott | @JW01: My comment is not a separate answer. It's clarification on this answer. It's the same thing: "Do not mark things as final unless you really, really mean it". | |
Jul 2, 2011 at 12:40 | comment | added | user1249 | @JW01, in a production setting you may not be ALLOWED to remove a final, because this is not the code the customer tested and approved. You may, however, be allowed to add extra code (for testing) but you may not be able to do what you want to because you cannot extend your class. | |
Jul 2, 2011 at 12:36 | comment | added | S.Lott |
@JW01: A final class has one primary use case. You have polymorphic classes that you do not want extended because a subclass might break polymorphism. Don't use final unless you must prevent the creation of subclasses. Other than that, it's useless.
|
|
Jul 2, 2011 at 12:27 | comment | added | JW01 |
Thanks S.Lott - I am trying to understand how bad use of final affects the code/ project. Have you experienced any bad horror stories that have lead you to be so dogmatic about the sparing use of final . Is this first hand experience?
|
|
Jul 2, 2011 at 12:18 | comment | added | S.Lott | I would say this more strongly. Do not mark things as final unless an extension cannot be made to work because of the design of the class. | |
Jul 2, 2011 at 12:18 | comment | added | JW01 |
Thanks. Could you explain that further. Do you mean that if I mark a class as final (a change) then I just have to re-test it?
|
|
Jul 2, 2011 at 12:12 | history | answered | user1249 | CC BY-SA 3.0 |