• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Anonymous Classes  RSS feed

 
Gary Fletcher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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?





 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Cheers guys..that's much clearer now..thanks..:-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!