• Post Reply Bookmark Topic Watch Topic
  • New Topic

A Method overriden in a method parameter?  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was following a tutorial for libGdx and suddenly became confused by the following syntax:



"up" is basically a text Button:



and .addListener is just one of the methods "TextButton" has (actually I think its inherited from "Button" but that doesn't matter).

Basically my question is what's going on inside the parentheses? From what I see its a new instance of "ClickListener" but then suddenly they override an actual method within. Is this simply just a way to override a method from the ClickListener class or is it something else?
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's way of creating a ClickListener instance while at the same overriding a method of it. The class has other methods (https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/utils/ClickListener.java), but the only one that's important in the context of a button is handling the button being clicked.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: this all changed in Java8.

What's happening is this: addListener() takes one parameter, an anonymous class. Its type is ClickListener which I believe is an interface, so you have to implement it. It has one method with the signature of public void clicked(InputEvent event, float x, float y) which you implement.

Java pre-8 didn't do anonymous methods, so the best is could do was an anonymous class. This idiosyncratic syntax allows you a shorter way than creating an inner class, implementing ClickListener and its method, instantiating an object of that type, and then passing the reference to that object to addListener.

Edit: when I started this post, Ulf hadn't posted yet.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid duplicated code, only use anonymous classes when you never do anything similar anywhere else in a Listener. Otherwise use named classes.
ClickListener appears to be a class designed for your Tutorial. Please send more details of it, so we can see how it works. I have never used a Listener which isn&apos't part of the standard API, but obviously overridden or as an anonymous class.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:ClickListener appears to be a class designed for your Tutorial. Please send more details of it, so we can see how it works.

Actually it's part of a Desktop/Android/HTML5/iOS Java game development framework. See the link in Ulf's post.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Joanne. I have had a look at it.
What OP is doing is taking an ordinary class and overriding one method, which you can do in an anonymous class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!