aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "anonymous inner class" Watch "anonymous inner class" New topic
Author

anonymous inner class

srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225


output is :

help your self
1

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
Sheriff

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
sscce.org
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: http://aspose.com/file-tools
 
subject: anonymous inner class