Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Is there a way to invoke new methods defined in Anonymous classes?

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the following code is there anyway i can call the new method called sizzle and get it to run? if it is not possible then could i please know the use of using anonymous classes?

class Popcorn {
public void pop(){
System.out.println("pop corn");
}
}

class Food {
Popcorn p = new Popcorn(){
public void pop(){
System.out.println("anonymous popcorn");
}
public void sizzle(){
System.out.println("fdsaf");
}
};
public static void main(String ar[]){
Popcorn p = new Popcorn();
p.pop();
p.sizzle();// does not compile. is there a way to get it to work?
}
}
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There might be a way using reflection, but that's probably a bad idea.

Simply don't use anonymous inner classes if you need to add methods, use them only to implement or override methods declared in the superclass/interface.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic