• 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

Using Anonymous Classes

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am just reading about anonymous classes an am getting a little confused.

I have 2 classes as shown below, the names are arbitary and simply for testing purposes. I am not able to compile and get the following compilation errors:

Description Resource Path Location Type
The method bMethod() is undefined for the type InnerLocalClass OuterClass.java /anonymousclasstest/src/anonymousclasstest line 31 Java Problem

As I understood it, I thought, the use of the anonymous class simply extends the SuperClass, InnerLocalClass in this case. If that is the case wouldn't the InnerLocalClass innerlocalClass = new InnerLocalClass(aInt){...} instanstiate a new object, innerlocalClass, that would simply be an extended version of InnerLocalClass? How do I access the bMethod() method? What am I doing wrong?





 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method bMethod() is defined in the anonymous class that you have in lines 19 - 29, but note that the type of the variable innerlocalClass in line 19 is InnerLocalClass.

That means that through that variable, you can only see the methods that are defined in class InnerLocalClass. Since bMethod is not defined in that class, you cannot access bMethod.

In fact, it is not possible to access methods that you define in an anonymous class in this way.

The only way to access bMethod in line 31 is by declaring it in InnerLocalClass, so that the bMethod in the anonymous class overrides the method in its superclass InnerLocalClass.

It's just like doing the following, without an anonymous class:

 
Gary Fletcher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhhhhh...I see. The book that I've been reading was a little misleading about that then.

Is declaring LocalInnerClass as abstract in this case allowed?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gary Fletcher wrote:As I understood it, I thought, the use of the anonymous class simply extends the SuperClass, InnerLocalClass in this case. If that is the case wouldn't the InnerLocalClass innerlocalClass = new InnerLocalClass(aInt){...} instanstiate a new object, innerlocalClass, that would simply be an extended version of InnerLocalClass?


Yup, but extension in this case is merely a tool used to create an unnamed class that behaves exactly like its superclass.

How do I access the bMethod() method? What am I doing wrong?


As Jesper said: you can't, because the class is anonymous.

Winston

PS: Please DontWriteLongLines. I've broken yours up this time, but it makes your thread very hard to read.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gary Fletcher wrote:Is declaring LocalInnerClass as abstract in this case allowed?


Yes. In fact, it's usually a good idea to do so. It's probably worth mentioning that you can also create anonymous classes from interfaces.

Winston
 
Gary Fletcher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers guys..that's much clearer now..thanks..:-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic