{"id":4297,"date":"2011-10-04T19:14:27","date_gmt":"2011-10-04T13:44:27","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4297"},"modified":"2022-01-12T19:01:29","modified_gmt":"2022-01-12T13:31:29","slug":"closure-as-an-implementation-to-interface","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/closure-as-an-implementation-to-interface\/","title":{"rendered":"Closure as an implementation to interface"},"content":{"rendered":"<p>Recently, I saw some code of grails Hibernate Plugin and noticed an interesting usage of &#8216;as&#8217; operator. The &#8216;as&#8217; operator of groovy is typically used to change the types of objects.<\/p>\n<p>For example:<br \/>\n[groovy]<br \/>\nint a = &quot;20&quot; as int<br \/>\nassert a==20<br \/>\n[\/groovy]<\/p>\n<p>The &#8216;as&#8217; operator can be used to provide implementation to interface as well. Here is an example of the same:<\/p>\n<p>[groovy]<br \/>\ninterface Eatable{<br \/>\n   void eat()<br \/>\n}<\/p>\n<p>def x =  {println &quot;It&#8217;s groovy style&quot; } as Eatable<br \/>\nx.eat()<br \/>\n[\/groovy]<\/p>\n<p>Had it been java, this could have been achieved through an annonymous class like this:<\/p>\n<p>[java]<br \/>\nnew Eatable(){<br \/>\n  void eat(){<br \/>\n    System.out.println &quot;Its java style&quot;<br \/>\n  }<br \/>\n}.eat()<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I saw some code of grails Hibernate Plugin and noticed an interesting usage of &#8216;as&#8217; operator. The &#8216;as&#8217; operator of groovy is typically used to change the types of objects. For example: [groovy] int a = &quot;20&quot; as int assert a==20 [\/groovy] The &#8216;as&#8217; operator can be used to provide implementation to interface as [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[358,845,426,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4297"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4297"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4297\/revisions"}],"predecessor-version":[{"id":54610,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4297\/revisions\/54610"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}