{"id":4511,"date":"2014-03-30T12:56:48","date_gmt":"2014-03-30T12:56:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/parameters-implementing-a-certain-interface-and-having-a-specific-superclass-generics-or-instanceof-collection-of-common-programming-errors\/"},"modified":"2014-03-30T12:56:48","modified_gmt":"2014-03-30T12:56:48","slug":"parameters-implementing-a-certain-interface-and-having-a-specific-superclass-generics-or-instanceof-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/parameters-implementing-a-certain-interface-and-having-a-specific-superclass-generics-or-instanceof-collection-of-common-programming-errors\/","title":{"rendered":"Parameters implementing a certain interface and having a specific superclass: generics or instanceof?-Collection of common programming errors"},"content":{"rendered":"<p>I would love some help on a particular design. This is the code I wish worked:<\/p>\n<pre><code>abstract class Square {...}\nabstract class Circle {...}\n\ninterface JPGExportable {...}\n\nclass SquareJPG extends Square implements JPGExportable {...}\nclass CircleJPG extends Circle implements JPGExportable {...}\n\ninterface Interface {\n    draw(Square square);\n    draw(Circle circle);\n}\n\nclass Canvas implements Interface {\n    draw(SquareJPG squarejpg) {...}\n    draw(CircleJPG circlejpg) {...}\n}\n<\/code><\/pre>\n<p>In words, Canvas should implement the specification that is Interface, BUT the draw-methods should <em>only<\/em> handle subclasses of Square and Circle that implement JPGExportable.<\/p>\n<p>To my knowledge there are two solutions that work, but neither of which I think is very pretty:<\/p>\n<pre><code>\/*\n * Using generics\n *\/\ninterface Interface {\n    draw(S square);\n    draw(C circle);\n}\n\nclass Canvas implements Interface {\n    draw(SquareJPG squarejpg) {...}\n    draw(CircleJPG circlejpg) {...}\n}\n\n\/*\n * Using instanceof\n *\/\ninterface Interface {\n    draw(S square);\n    draw(C circle);\n}\n\nclass Canvas implements Interface {\n    draw(Square square) {\n        if (square instanceof SquareJPG)\n            \/\/ do stuff now\n    }\n    draw(Circle circle) {\n        if (circle instanceof CircleJPG)\n            \/\/ do stuff now\n    }\n}\n<\/code><\/pre>\n<p>In reality Square and Circle are fairly distinct, why a common super-class wouldn&#8217;t be able to contain any common code. Also, a super-class implementing JPGExportable would feel&#8230; wrong; it&#8217;s really a sub-feature.<\/p>\n<p>An underlying reason why I don&#8217;t like the generics-way is that I need to handle 7 different types. Perhaps I&#8217;m being picky, but 7 occurences of &#8220;T extends Type&#8221; in a row looks ugly.<\/p>\n<ul>\n<li>Is there a third solution which looks better?<\/li>\n<li>If not, which of the two &#8220;is better&#8221;?<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I would love some help on a particular design. This is the code I wish worked: abstract class Square {&#8230;} abstract class Circle {&#8230;} interface JPGExportable {&#8230;} class SquareJPG extends Square implements JPGExportable {&#8230;} class CircleJPG extends Circle implements JPGExportable {&#8230;} interface Interface { draw(Square square); draw(Circle circle); } class Canvas implements Interface { draw(SquareJPG [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4511","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4511"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4511\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}