How often do you see or use the 'final' keyword?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
ThisSiteHasNoHttps
Forum Newbie
Posts: 3
Joined: Fri Oct 06, 2017 11:06 am

How often do you see or use the 'final' keyword?

Post by ThisSiteHasNoHttps »

I was asked this on a job interview recently: Can you extend a class defined with the 'final' keyword?

I'd hardly ever seen that keyword used (only in pre-existing framework code, not from any developer I knew), so I tentatively answered 'no', but he said the answer was 'yes'.

However, I looked it up later and found out I may have been correct:
"If the class itself is being defined final then it cannot be extended."
http://php.net/manual/en/language.oop5.final.php

I don't remember exactly how the interviewer worded it, but I haven't been able to find any exceptions to this rule, and I wonder if he may have meant "extend a method", which in my research doesn't make any sense because you don't extend a method, you extend a class, and override a method, and 'final' on a method just means you can't override a method...

So if by "extend a method" he meant "override a method by 'extending' its code", I still don't think that's possible the way I understand it.

Have you ever used or seen the 'final' keyword used by any developer you knew, or how often? My PHP 5 book doesn't have it in its index (and php.net says it was implemented in PHP 5), and I don't think I've seen a real-world work case where I or any developer I worked with needed to care about it or even that it got in the way of anything, so I'm considering the question a corner/trick/minutia question at this point.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How often do you see or use the 'final' keyword?

Post by requinix »

A final class cannot be extended and a final method cannot be overridden in a child class, so either you're not remembering the question right or the interviewer was wrong.

It doesn't get used a whole lot, but there are times for it. Mostly it comes down to situations where a particular method implementation is fixed and should never be altered by a child. I'm drawing blanks on a good example of that. It's also used for "static" classes in PHP: a final class with a private constructor, so the class is never instantiated and it is only useful for static method calls.

\Exception is an example of a class with final methods. I've used it a number of times myself.

In OOP you generally want to allow for the possibility of child classes and method overriding, since that's like 95% of the power of OOP, so not a lot is explicitly marked as final. (Ironically, Exception is one of those classes you often should extend.) But that certainly wasn't a trivia question: final, abstract, static, those are all important concepts in OOP and knowing what they are demonstrates that you do, in fact, know object-oriented programming - even if you don't use them all very often.
Last edited by requinix on Fri Oct 06, 2017 12:36 pm, edited 2 times in total.
Reason: more
ThisSiteHasNoHttps
Forum Newbie
Posts: 3
Joined: Fri Oct 06, 2017 11:06 am

Re: How often do you see or use the 'final' keyword?

Post by ThisSiteHasNoHttps »

requinix wrote:A final class cannot be extended and a final method cannot be overridden in a child class, so either you're not remembering the question right or the interviewer was wrong.
Thanks, but my answer was based on what I heard at the time, not what I remembered afterword. Either I misheard it or he misstated it or one of us misunderstood the question.
requinix wrote:It doesn't get used a whole lot, but there are times for it. Mostly it comes down to situations where a particular method implementation is fixed and should never be altered by a child. I'm drawing blanks on a good example of that. It's also used for "static" classes in PHP: a final class with a private constructor, so the class is never instantiated and it is only useful for static method calls.

\Exception is an example of a class with final methods. I've used it a number of times myself.

In OOP you generally want to allow for the possibility of child classes and method overriding, since that's like 95% of the power of OOP, so not a lot is explicitly marked as final. (Ironically, Exception is one of those classes you often should extend.) But that certainly wasn't a trivia question: final, abstract, static, those are all important concepts in OOP and knowing what they are demonstrates that you do, in fact, know object-oriented programming - even if you don't use them all very often.
If `final` generally goes against the power of 95% of OOP, then I don't see it's anywhere near as important as abstract or static (though I of course can see where it would be useful); I don't like repeating myself, but I've never seen it used by any of the dozens of developers I've worked with, only in frameworks and some documentation (very occasionally), and it wasn't even in the PHP 5 book I own. `abstract` and `static` I've seen a lot... so yah, I consider an interview question about a hardly-used keyword like `final` to be _trivial_ (not 'trivia' as you said), and even if someone knows nothing about it and sees it, they can easily look it up and it's easy to understand.

Trick/trivial/corner questions can really screw up an interview, and to me it's a plinko game of little value. You could run into someone who knows a few niche things and is terrible at code structure or concepts or figuring things out, I've just never thought these kinds of 'gotcha!' questions are useful to any kind of interview.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How often do you see or use the 'final' keyword?

Post by requinix »

Not knowing about final is one thing, but as an interviewer I would rather you have told me you don't know: you're right, it can be easily looked up, but a good interviewer knows how to use technical questions to test for more than technical knowledge. Knowing bits and pieces of OOP (or anything really) paints you into a corner in terms of what you can do, and if you don't even know about some features then you won't be able to use them in even the 5% of situations that it makes sense to do so.

You're allowed to think that final is a trivial feature, but in exchange I (as an interviewer) am allowed to think that you don't know much OOP or haven't really tried to learn what it can do. After all, I described what it does in a single sentence and the PHP doc page is only one screen long, so if that wasn't worth your time then how I can know what is? What will happen when you (eg) want to extend or override something final and PHP won't let you? What other aspects of PHP have you not come across? I simply don't know. Remember, it's an interview: I have a limited amount of time to gauge who you are as a programmer, and getting into a discussion of why you do or do not know something isn't an option. I'm not saying it disqualifies you as a job candidate, but it doesn't help your chances.
ThisSiteHasNoHttps
Forum Newbie
Posts: 3
Joined: Fri Oct 06, 2017 11:06 am

Re: How often do you see or use the 'final' keyword?

Post by ThisSiteHasNoHttps »

requinix wrote:Not knowing about final is one thing, but as an interviewer I would rather you have told me you don't know: you're right, it can be easily looked up, but a good interviewer knows how to use technical questions to test for more than technical knowledge. Knowing bits and pieces of OOP (or anything really) paints you into a corner in terms of what you can do, and if you don't even know about some features then you won't be able to use them in even the 5% of situations that it makes sense to do so.
I did know about final; if I'd said I didn't know, that would have been lying. I answered the question as I knew it, and it appears PHP documentation showed I was right.
requinix wrote:You're allowed to think that final is a trivial feature, but in exchange I (as an interviewer) am allowed to think that you don't know much OOP or haven't really tried to learn what it can do. After all, I described what it does in a single sentence and the PHP doc page is only one screen long, so if that wasn't worth your time then how I can know what is? What will happen when you (eg) want to extend or override something final and PHP won't let you? What other aspects of PHP have you not come across? I simply don't know. Remember, it's an interview: I have a limited amount of time to gauge who you are as a programmer, and getting into a discussion of why you do or do not know something isn't an option. I'm not saying it disqualifies you as a job candidate, but it doesn't help your chances.
Answering a question correctly and then being told it's incorrect implies nothing about how much I know about OOP. It's a yes/no question. The documentation showed he's wrong, and I never said the documentation was wrong (it justified my answer when I looked at it).

You're making a lot of (negative) assumptions here unfortunately, which is not even worth trying to argue over.

I guess we live in a day and age where you can't leave an answer without trying to make yourself feel superior to someone; it's unfortunate your verbiage here seems to be skimming what I've said and just taken the "he must not know what he's talking about" kind of responses. I'm not going to see the reply to this, sorry not sorry.
Arynews
Forum Newbie
Posts: 1
Joined: Tue Mar 20, 2018 2:05 am

Re: How often do you see or use the 'final' keyword?

Post by Arynews »

A final class cannot be extended and a final method cannot be overridden in a child class, so either you're not remembering the question right or the Latest Pakistan News
interviewer was wrong.
Post Reply