This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Understanding method declarations created with Object ob = new Object(){ ... };