File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

anonymous inner class

 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ahmed yehia
Ranch Hand
Posts: 424
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic