• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Anonymous Inner Class

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Sierra/Bates, Chapter 8:




The code compiles and runs, but does not print "foofy". Based on previous discussions, I have tried several things:



Compiler outputs:

MyWonderfulClass.java:12: incompatible types
found : void
required: MyWonderfulClass
MyWonderfulClass my = new MyWonderfulClass().go();
^
MyWonderfulClass.java:13: cannot find symbol
symbol : method doStuff()
location: class MyWonderfulClass
my.doStuff();
^
2 errors


Then I tried:



Compiler outputs:

MyWonderfulClass.java:13: void cannot be dereferenced
my.go().doStuff();
^
1 error




What can I change in the main() method such that the code will print foofy? Inner classes seems more challenging to grasp.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when we are creating the anonymous class at the method argument
we are directly creating the object of the class
when we are declaring the method

then what do we do here?
it states that the method argument variable f of type Foo is referring to the object that is passed to this method
now here
when we actually pass the argument to the method


we know that the method foof() is of the class (actually anonymous class here ) that implements the interface Foo
now
we know that the method foof() can be called only by using the reference of type class that implements Foo interface
according to our program only the reference variable f in the method declaration doStuff(Foo f) can refer to such class the implements Foo interface
polymorphism applies here
we are referring to the subclass (in this case the implementing class) object using the super type reference (here the reference is interface type)'
to call the method foof() we can use only the variable f
and also the variable f will be available only for the method doStuff() as that is the method argument
so we call the foof() method in doStuff() method as follows


this should solve the problem


Now let us talk about the things you have done wrong in your program

the code means
1. Create a variable my of type MyWonderfulClass
2. Create a new object of type MyWonderfulClass and call the go method of that class
3. notice here that the go() method returns nothing i.e. the return type of the method is void
4. and using the assignment operator you are trying to assign a void type to MyWonderfulClass type (notice here that null and void are totally different)

this causes the error incompatible types
and also
doStuff() method belongs to the class Bar and you are calling it using the my variable hence the error cannot find symbol as the MyWonderfulClass does not contain doStuff( )method

also
the code


cannot compile because here again you made the same mistake
go() returns void and we cannot call doStuff() method using the void (void cannot be deferenced)
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasad,

Thank you for the explanation. I had to re-read it several times, and I somewhat grasp it (I think inner classes are at the same complexity as Regular Expressions ).

I re-wrote the code, which compiles and runs:



I just don't understand why the Sierra/Bates book didn't include f.foof() in the method doStuff() in class Bar, i.e.



Is there any other way this code would work without including the f.foof()? That is what I'm trying to experiment with right now...



 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well the variable f is available only in the method as it acts as the local variable (but it is not local variable, it is argument)
so we cannot access the variable outside the method but still if we want to
then we have to do some ridiculous things
try following code
change the main method to

and the Bar class to

Hope this helps
Happy preparation
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic