• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

A anonymous class question

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone.I got a question from Marcus Green'a mock 1
Question 34)
Which of the following statements about this code are true?
public class Morecombe{
public static void main(String argv[]){
Morecombe m = new Morecombe();
m.go(new Turing(){});
}
public void go(Turing t){
t.start();
}
}
class Turing extends Thread{
public void run(){
for(int i =0; i < 2; i++){
System.out.println(i);
}
}
}

1) Compilation error due to malformed parameter to go method
2) Compilation error, class Turing has no start method
3) Compilation and output of 0 followed by 1
4) Compilation but runtime error
its seemed that line 4 ,
m.go(new Turing(){});
make a anonymous class "Turing(){}",i thought it different from the below one .But after i took the "{}" away,its still works well.
Dos "{}"has nothing to do with it?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Turing is a concrete class.
If the argument to the go method is
new Turing()
then we just created a new Turing object and passed a copy of a reference to it to the go method.
If the argument to the go method is
new Turing(){}
then it is a anonymous subclass instance of class Turing that has been just created and passed as argument to the go method.
For instance, you could do that if you wanted to change the behavior of the run method in Turing but you would have to provide a new run method implementation within the curly braces.
However, if Turing was an abstract class which would have at least one abstract method, the second alternative would not compile, since the anonymous inner class must implement all abstract class of the class it extends.
[ March 22, 2002: Message edited by: Valentin Crettaz ]
 
guo mark
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait,wait.Is that you mean that in line 4 we got a subclass instance of Turing?But how this happen ?The subclass extends the Turing by itself?
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
That's what inner classes are all about, in fact you can also do new ActionListener(....) in a method and implement an Interface w/o using the keyword "implements" and w/o even realizing it
Too much fun, NO?
HTH,
- Manish
[ March 22, 2002: Message edited by: Manish Hatwalne ]
 
guo mark
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool,i think inner class is too smart now . :roll:
[ March 22, 2002: Message edited by: guo mark ]
 
Don't listen to Steve. Just read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic