• 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

How to invoke a method in the(flavour one) annonymous innerclass?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In kathy sierra and Bert Bates book in annonymous inner classes topic,in page num 470 the code is as follows

class popcorn
{
public void pop()
{

System.out.println("popcorn");

}

}

class food
{

popcorn p=new popcorn()
{
void sizzle()
{

System.out.println("sizzle in annonynous");

}

public void pop()
{
System.out.println("pop in annonynous");
}


};
public void popIt()
{

p.pop();
//p.sizzle();
}

}

My doubt is how to invoke the method sizzle() in annonymous inner class?

I tried to create an object of type Food,and tried to invoke sizzle() on it,as the annonymous class is in Food.

But getting compile error sayong that it cant find the method sizzle() in Food class.


How to do it?
 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My doubt is how to invoke the method sizzle() in annonymous inner class?

I tried to create an object of type Food,and tried to invoke sizzle() on it,as the annonymous class is in Food.

But getting compile error sayong that it cant find the method sizzle() in Food class.



See, here you are creating subclass for class popcorn and not for food class.Though your annonymous class is in Food.But see the type of p i.e here it is of type popcorn.
popcorn p=new popcorn()
{
....
}

says that p will start referring to subclass of popcorn(subclass of popcorn has no name, therefor anonymous).

now since superclass i.e popcorn donot have sizzle() method, then howcome compiler will allow you to call p.sizzle(); see the reference is of type popcorn.

its somewhat like

class popcorn
{

pop()
{
}
}

class Anonymous extens popcorn
{
pop()
{
}
sizzle()
{
}

class mains
{
public static void main(String[] args[])

{
popcorn p=new Anonymous();
p.sizzle(); //will it compile??? NEVER since referece is of type popcorn
//and compiler will complain that it dont know about any
//sizzle() method() in class popcorn;

p.pop();//however it will work,and at runtime sublcass's pop() will work
}
}

Hop you are clear...
 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Maanshenoy,

What Iam asking is is there anyway to invoke sizzle() which is in annonymous inner class?

If so,how?
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to have sizzle method in popcorn class.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

vatsalya:

is there anyway to invoke sizzle() which is in annonymous inner class?

you simply can't.
Because if you want to invoke a method, you need an object of this class (possible with your anonymous class) AND you need to have a variable that has the type of that class.
The latter is impossible, because the anonymous class is - ehem - anonymous. Thus not having an own type.
How could you code it?

You might invoke the sizzle() method by class introspection but this is totally meaningless reagarding the exam (because it is far beyond its scope). And I wouldn't care about this anyway.


Yours,
Bu.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vatsalya,

You can think of the code in the following way.
While you are creating an anonymous class this way, you are actually creating a subclass of the class "Popcorn" but you still have reference variable of the superclass.
And as you know you can't invoke any method added in the subclass (but absent in the superclass) with a superclass reference.
This kind of anonymous class will benefit only if you try to override the implementation of a superclass method without creating any concrete class separately.

Thanks,
Sudipto.
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic