This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes anonymous inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "anonymous inner class" Watch "anonymous inner class" New topic

anonymous inner class

srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225

output is :

help your self

but i was expecting 1, even if it is overriding then only help your self
should be printed but its printing both. explain this.

SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
marc weber

Joined: Aug 31, 2004
Posts: 11343

"help your self" is printed as part of the overridden method, which returns an int value of 1. That returned value is printed in the println statement in main.

Tip: Some people find it very helpful to indent code. I think you might get better responses here if your code was easier to read.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
ok marc from next time i will indent the code.

But here is it overriding, i am sending an reference of popcorn class so it should return 1 which is working fine. but why is the sizzle() method of the anyonymous innerclass is called.
Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi Srinivas,
Object type (not the reference variable's type), determines which overridden
method is used at runtime.
Reference type determines which overloaded method will be used at
compile time.

So in your program also same thing is happening.

SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

but why is the sizzle() method of the anyonymous innerclass is called

Because the anonymous class, is a sub class of the orignal class.
You are using a reference variable of type 'popcorn' but it actually refer to this anonymous subclass, so polymorphism apply.
I agree. Here's the link:
subject: anonymous inner class
Similar Threads
Inner classes
Anonymous inner class
K&B Anonymous Inner Class.
Anonyms Inner Class ????
Is there a way to invoke new methods defined in Anonymous classes?