0

I was reading an example about final for Java, and saw something like this:

Counter c = new Counter()
{
  public int count() {
    super.count();   
    return super.count();
  }
}
c.count(); // now count 2

Is there a name for such construct? Is this ever used in practice? Is yes, it is considered a good OOD? A reference or a blog article would be appreciated.

Community
  • 1
  • 1
Forethinker
  • 3,548
  • 4
  • 27
  • 48
  • 2
    is [Anonymous Classes](http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html) what you are talking about? – Karthik T Sep 11 '13 at 05:04
  • 1
    Its a commonly used hack(?) for passing functions to other functions, but can also be used as you show to make subclasses which are used only once – Karthik T Sep 11 '13 at 05:05
  • 1
    Thanks Karthik. This is indeed called an anonymous class and its uses are outlined [here](http://stackoverflow.com/questions/355167/how-are-anonymous-inner-classes-used-in-java). Thanks again. Marking this question as a duplicate. – Forethinker Sep 11 '13 at 05:08
  • I wouldn't call it a hack. – rocketboy Sep 11 '13 at 05:12
  • @Forethinker First time I have seen somebody agreeing that a question is duplicate. Good Job mate :) – amod Sep 11 '13 at 05:27

0 Answers0