TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Nuances of overloading and overriding in Java

87 pointsby whackabout 2 years ago

14 comments

shagieabout 2 years ago
Varargs methods in Java was one that bit me with an overload in the past (my tale of is told in more detail at <a href="http:&#x2F;&#x2F;the-whiteboard.github.io&#x2F;java&#x2F;debugging&#x2F;warstory&#x2F;2016&#x2F;07&#x2F;27&#x2F;dont-overload-varargs.html" rel="nofollow">http:&#x2F;&#x2F;the-whiteboard.github.io&#x2F;java&#x2F;debugging&#x2F;warstory&#x2F;2016...</a> )<p>The issue was that there was a framework class with the method signature of execute(Object ... inParams). The class had overloaded the method signature to be execute(String foo, String bar). If you called it with two args, it got the overloaded call and there were a half dozen calls to this method.<p>At some point, someone added another argument to this so that it was execute(String foo, String bar, String qux) and updated the half dozen less one methods to use the third parameter.<p>However, the two args call in some other code still compiled correctly because it now went back to calling execute(Object ... inParams). Though, the parameters were completely wrong for that call structure and the application suddenly sprouted runtime errors.
评论 #35994536 未加载
评论 #36012581 未加载
exabrialabout 2 years ago
Most of these rules are pretty straigthtforward. I don&#x27;t really find any surprises in the Java method dispatch system, it just sort of works without a lot of thinking about it.<p>The one area I think leaves some question marks for me is the &quot;new&quot; default methods in interfaces, aka &quot;public defender methods&quot;.<p>They were introduced awhile back, but they&#x27;ve mostly been avoided until recent years. As they become more common, a thorough understand of how Java deals with double-diamond needs to be understood by Java developers.
martincmartinabout 2 years ago
Many of these things are at the core of the book Java Puzzlers.<p><a href="http:&#x2F;&#x2F;www.javapuzzlers.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.javapuzzlers.com&#x2F;</a>
lern_too_spelabout 2 years ago
None of this is surprising. Just think about how the compiler is going to use the object&#x27;s vtable. There is no magic going on.
评论 #35991403 未加载
评论 #35990554 未加载
评论 #35995710 未加载
mrkeenabout 2 years ago
I&#x27;m disappointed that<p>&gt; The method that gets invoked depends on the “actual” instance type, not the “declared” instance type.<p>doesn&#x27;t apply more broadly. If you have a call(fruit) method and a call(apple) method, when you supply a fruit, it will resolve to call(fruit), even if the fruit instance is actually an apple.
评论 #35993442 未加载
评论 #35993254 未加载
aymar_99about 2 years ago
Thanks for this curation, useful for me as a beginner. All the comments belittling these puzzles kinda feel rude.
invalidnameabout 2 years ago
Are these things HN readers didn&#x27;t know?<p>I didn&#x27;t know you can do operator overloading in Java (with Manifold). That would be interesting: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=pwQs-308OdY">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=pwQs-308OdY</a>
评论 #35993004 未加载
评论 #36012930 未加载
tmtvlabout 2 years ago
Reminds me of CLOS and the versatility of <i>defgeneric</i> and <i>defmethod</i>.
agumonkeyabout 2 years ago
Makes you appreciate the complexity of haskell.
yotamoronabout 2 years ago
I hate inheritance.
评论 #35992527 未加载
bvanderveenabout 2 years ago
Just say no.
adelarsqabout 2 years ago
These are one of the things that shiver me in production legacy code.
vanillaxabout 2 years ago
&quot;half a decade&quot; isn&#x27;t a brag. Its overselling the fact that you coded for 5 years. Big whoop. Stop being a wordcel and making it seem like you are more qualified than others in the space because you used the term &quot;half a decade&quot; like its some large quantity of time.
cutlerabout 2 years ago
A good reason, if any were needed, to escape from the Java OOP maze once and for all and never return. Honestly, talk about a mountain of incidental complexity. People, wake up! It doesn&#x27;t have to be this way. You&#x27;ve been sold Java&#x27;s absurd contortions dressed in the emperor&#x27;s new clothes as the default by universities and big corps for decades. There&#x27;s a whole other world out there where data is .... just ..... data.
评论 #35993496 未加载
评论 #35993157 未加载