aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to invoke a method in the(flavour one) annonymous innerclass? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to invoke a method in the(flavour one) annonymous innerclass?" Watch "How to invoke a method in the(flavour one) annonymous innerclass?" New topic
Author

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

vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
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?
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
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...


Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
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

Joined: Dec 20, 2007
Posts: 299
you need to have sizzle method in popcorn class.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
deep majumder
Greenhorn

Joined: Jun 11, 2007
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to invoke a method in the(flavour one) annonymous innerclass?