Re:If Inheritance is so bad, why does everyone use it?
by kaba0 on Friday April 12, 2024 @07:30AM
(#40011380)
Java has generic functions since forever, something like `<T extends Fruit> T rotateFruit(T fruit)` would return your banana typed object just fine.
Re:If Inheritance is so bad, why does everyone use it?
by skydhash on Friday April 12, 2024 @08:57AM
(#40012071)
Inheritance is just a tool, that you use if it fits you software architecture. You use other tools, like generics, interface, where it makes senses to ise them. I think people wants the one true way to not have to design their software. And when they use inheritance in a ways that does not fit, they blame inheritance.
Re:If Inheritance is so bad, why does everyone use it?
by mrkeen on Saturday April 13, 2024 @04:23AM
(#40021459)
But then I lose polymorphic dispatch, which is where this thread started.
Re:If Inheritance is so bad, why does everyone use it?
by kaba0 on Saturday April 13, 2024 @05:33AM
(#40021765)
If you pass your object to another function, you only get static dispatch. What you want instead in this case, is a simple instance method. Then yourApple.rotateFruit() would do what you want, and rutateFruit could be an interface method declared in Apple, the superclass/interface.
I would never. I know that I'm holding a banana, I want to pass it to a method that receives a banana. But what I can't do is put my banana through a rotateFruit function first, because then Java will forget that it's a banana and start treating it as a fruit.