• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Method overriden in a method parameter?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic