Tuesday, January 13, 2009

Implementing interfaces in Groovy

This a quick post just to put some facts out there after i read this post.

In the post the Vita states that "In Groovy when your class implements a Java interface it must have all the methods present"

Well this is NOT exactly true. Here is an example below of me implementing only the interfaces i want:

This is my very simple interface with 3 methods;

public interface SaySomething {

String saySomethingTrue();

String saySomethingFalse();

String saySomethingFunny();
}


In Groovy you easily implement a multi-method interface with a Map (method name = closure)

So i have code like this. I ONLY implement the methods i want.

SaySomething saySomething = [saySomethingTrue:{'Groovy rocks'},
saySomethingFalse:{'Groovy must implement all methods in a Java interface.'},] as SaySomething;

println "This is true: " + saySomething.saySomethingTrue();

println "This is false: " + saySomething.saySomethingFalse();


As you can see in quick example above i don't provide an implementation for saySomethingFunny()

The result of running this is:

This is true:  Groovy rocks
This is false: Groovy must implement all methods in a Java interface.


UPDATE...

If you just want to do the same thing for all methods:

SaySomething saySomething2 = {return "I am handling all methods"} as SaySomething

println "This is true: " + saySomething2.saySomethingTrue();

println "This is false: " + saySomething2.saySomethingFalse();

println "This is funny: " + saySomething2.saySomethingFunny();


This prints out:

This is true:  I am handling all methods
This is false: I am handling all methods
This is funny: I am handling all methods

25 comments:

chanwit said...

it's true *behind* the scene.

Luke Daley said...

You really should state what happens when you call one of the interface methods that you don't define.

Peter Delahunty said...

Hi Luke

I update the blog entry to show handling all method calls from one closure.

In the case you said. An error would occur if you tried to call a method that does not have and entry in the map.

Anonymous said...

My take on the same problem
http://groovyconsole.appspot.com/script/522001

help with dissertation said...

Not quite time to get acquainted. What he saw impressed.
I liked the section on educational programs on the internet. I would like to know more about programming in HTML.

niki 3254 said...

I like your site very much, I fall here by google search and start reading your post, by watching all pages I get to know that you are posting nice stuff on your pages,Edinburgh meet and greet I would share this information with all my friends, as you are providing a nice information resource to all people, this is only one place I seen this much information all together, because people search for such places where they can get all they looking for. Im really happy to find this place and I would talk about your site with my friends. I would definitely return back to your site as I book marked your page url to visit you again.Bristol meet and greet I hope you will keep posting nice stuff on different important issues, and will share all your nice stuff with us, and all your experience to give more knowledge to people, the design of your website is really nice and you are working hard to provide a best information resource. No doubt that you are one of the best people who are working hard to make this internet world more worth full as there are many website who are not providing that they should. They are just trying to waste people time and give no good results. But im really happy with your site and design you are using is very good.I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. I really loved reading your blog. It was very well authored and easy to understand. Unlike additional I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he enjoyed it as well!Thanks so much for this! I have not been this thrilled by a blog post for quite some time! You’ve got it,Glasgow meet and greetwhatever that means in blogging. Anyway, You’re definitely someone that has something to say that people should hear. Keep up the wonderful job. Keep on inspiring the people.

Anish Ahmad said...

I wanted to thank you for this great read!! I definitely enjoyed every little bit of it. I have you bookmarked to check out new stuff on your post.
raspberry ketone | raspberry ketone side effects | acai berry weight loss

Anish Ahmad said...

It was very useful for me. Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing such a information with us.
anti wrinkle cream for men | sizegenetics reviews

marya lusy said...

Hello webmaster good day. Superb post. You have gained a new fan. Please keep up the good work and I look forward to more of your great posts.
resource | visit site | Good resource for weight loss

marya lusy said...

Your post will be rather good and I'm sure some will find it interesting because it's about a topic that's as widely discussed as others. Some may even find it useful.Thanks so much for your post.
web site | official site

marya lusy said...

Thanks for sharing this useable article - I really increase your acquisition.it is really very informatic post for young people, and hope they will enjoy well after reading this post.
breastenlargementpill.webs | naturalbreastenlargementpills.tripod.co.uk

marya lusy said...

Nice post and right to the point. I am not sure if this is actually the best place to ask but do you folks have any ideea where to hire some professional writers? Thanks in advance
garcinia | garciniacambogiawheretobuy1.webs.com

Akram Ahmad said...

Hi, I do think this is a great site. I stumbledupon it ;) I may revisit yet again since i have saved as a favorite it.
where to buy phen375 | where can i buy pure garcinia cambogia | pure green coffee bean extract reviews |

nadeem khan said...

Nice post and right to the point. I am not sure if this is actually the best place to ask but do you folks have any ideea where to hire some professional writers? Thanks in advance 


Reviews on Raspberry Ketones |Review on Garcinia Cambogia
review on green coffee extract |review on green coffee bean

Arlington VA Used Cars said...

Connections can be used to implement the Bequest relationship between the non-related classes that do not linked with the same framework, i.e. any Classification and any where in framework.

Karyn Scott said...

Using Interface, you can specify what a category must do but not how it does.Learn More Online

Ida @Cosmetic Surgery Bellevue said...

If an Interface does not specify the exemption record for a technique, he category can not toss any exemption list

jone smith said...

I admire the valuable information you offer in your content articles. I will bookmark your weblog. I am quite certain i will learn lots of new stuff right here than anybody else!


auto dealer los angeles.

simcoe smith said...

You should try to check state what happens when you code for the interface methods for call which you don't define. http://www.escortsinmanchester.co.uk

Ronny678 said...

Our buy period paper writers only use the details and figures to evolve notions but do not exact replicate and paste on your buy time span paper paper. buy initial paper, all our time span paper composers compose your time span buy essays to buy from rub. From our period paper conceiving enterprise, you buy cheap essay.

Speicher Spe said...

Since medical suppliers exert a robust influence over the number and kinds of treatment demanded,If you learn more befitting response about inversion table reviews pleasure appear free to visit our site.

Speicher Spe said...

My tuition was paid, however i used to be able to do therefore as a result of i used to be budgeting, I drove a clunker, and that i had roommates. Singapore Loan

Runi said...

These folks have a large human capital as a result of they possess skills nobody else has, and therefore these scarce skills provide them the chance and the advantage to become wealthy. They get paid plenty of cash as a result of nobody else will do what they are doing at the high level they are doing. MLM

Dante Alfere Condori said...

and with two methods overload (diferents params)?

lucia789 said...

It appears that they will always have their website as their first choice which will be helpful in presenting them outstanding and diverse content. It is a fact that once you will have the exceptional and associated content then no one can come in the way to attract more visitors towards your website. denverseo.pw