3 ms·
I think protected methods are just a bad idea in every case. They don't really offer protection (because a subclass can expose them) and they prevent other for
by emn13 13y ago
I think protected methods are just a bad idea in every case. They don't really offer protection (because a subclass can expose them) and they prevent other forms of composition even where they're more appropriate.
If you're open to inheritance, you're necessarily open to composition - making that messy serves no purpose.
As to this example:
// Replace superclass method
A.prototype.bar = function() {
return "my own thing"; // obviously fine
}
This is NOT fine - it's really quite nasty. You're breaking encapsulation by affecting how the internals of B work - calls from B's code to bar() will now fail to work as expected. What you want is to use your bar to outside code, but not affect the encapsulation of the superclass.
And note that none of this mitigates the pit-of-complexity that inheritance encourages as described in the post you replied to (i.e. bloated, hard-to-wrap API's). There are inheritance-like techniques that work better and don't have the downsides.
- jdlshore 13y agoI don't agree, but I'm going to let it drop. I just wanted to reply to say "thanks" for engaging in a thoughtful conversation. Somebody's been downvoting thoughtful posts like yours (because they disagree with them, I guess). I wish they'd stop and I wanted to let you know it wasn't me. :-)
- emn13 13y agoHey, thanks for the friendly sign off! A much nicer way to end a conversation. Given the subtly of these issues (what is the right design of such an artificial construct), I guess the only obvious thing is that there's no obviously right answer - let alone that it's easy to explain the pros+contras in a reasonable amount of time on an online forum such as this :-).