*
The moose likes Beginning Java and the fly likes Understanding method declarations created with Object ob = new Object(){ ... }; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding method declarations created with Object ob = new Object(){ ... };" Watch "Understanding method declarations created with Object ob = new Object(){ ... };" New topic
Author

Understanding method declarations created with Object ob = new Object(){ ... };

Ehsan Rahman
Ranch Hand

Joined: Feb 16, 2009
Posts: 59


Hi All,

This is probably an easy one ...



Why is it that if line 5 is commented out then the method in 11 is not applicable (and results in a compiler error), even though it does override as the code is above.

I've noticed this type of over-ride used a lot with the run() method when creating Threads.

Many Thanks,
Ehsan


SCJP 1.5, SCJD 1.6
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
The @Override annotation lets the compiler know that you're overriding or implementing a method in a superclass. By commenting out the original method, you're no longer overriding it in your anonymous subclass.

John.
Ehsan Rahman
Ranch Hand

Joined: Feb 16, 2009
Posts: 59

Thanks. Yes, it is an anonymous class.

http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm
Syntax: new class-name ( [ argument-list ] ) { class-body }

http://www.coderanch.com/t/409946/Java-General-beginner/java/call-method-anonymous-class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding method declarations created with Object ob = new Object(){ ... };
 
Similar Threads
static synchronized vs non-static synchronized
Overriding with Exception
garbage collection
Shutdown hook
Thanks everyone.