PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Oct 24, 2017 2:49 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Oct 06, 2017 11:15 am 
Offline
Forum Newbie

Joined: Fri Oct 06, 2017 11:06 am
Posts: 2
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.


Top
 Profile  
 
PostPosted: Fri Oct 06, 2017 12:29 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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.
more


Top
 Profile  
 
PostPosted: Sat Oct 07, 2017 7:10 pm 
Offline
Forum Newbie

Joined: Fri Oct 06, 2017 11:06 am
Posts: 2
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.


Top
 Profile  
 
PostPosted: Sun Oct 08, 2017 8:28 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group