Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Issue

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I heard that inheritance concepts can be achieved by anonymous class(without using extends and implements keyword). Anonymous class will not have the class name. How could we inherit some of the methods or variables of the anonymous class from the other class.
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you are asking.
Here's a simple anonymous class(off the top so many have syntax errors):


The paint() method can reference variables in the outer class with the proper coding.
I don't know how the outer class can reference variables in the inner class such as the x above??
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could we inherit some of the methods or variables of the anonymous class from the other class.

Methods, etc. are inherited the same way as with non-anonymous classes. Ex:
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the reply. However, It seems to be confusing. Do you know any online tutorials for anonymous class for beginners other than Sun's tutorial.

I am having doubt on ur pgm:


Usually, in thread, we will create the thread object and by calling the start(), the run method will invoke automatically. And also we have to implement the Runnable interface or extends the Thread class for Thread.
The code I can't understand were marked in Bold. If u don't mind can u pls explain me clearly, as if i were a beginner.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code



defines an anonymous class that implements the Runnable interface and creates an instance of it. It's vaguely equivalent to



So that when you put this code into the code you show above, it's similar to

new Thread(new MyRunnable()).start();

which itself is shorthand for

Runnable r = new MyRunnable();
Thread t1 = new Thread(r);
t1.start();

Does this make more sense now?
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. Now it is very much clear!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic