• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

does an anonymous inner class have constructor?

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which two are true?
A. An anonymous inner class can be declared inside of a method
B. An anonymous inner class constructor can take arguments in some situations
C. An anonymous inner class that is a direct subclass of Object can implements
multiple interface
D. Even if a class Super does not implement any interfaces, it is still possible
to define an anonymous inner class that is an immediate subclass of Super that
implements a single interface
E. Even if a class Super does not implement any interfaces, it is still possible
to define an anonymous inner class that is an immediate subclass of Super that
implements multipe interface
Answer:A,D
question:
1.does an anonymous inner class have constructor?
2.can an anonymous inner class take arguments ?
like Pop p = new Pop(int a ,int b){...};
3.is d right? "an anonymous inner class can't evern extend a class and implement an interface at the same time" p472 of the"Sun Certificatied Programmer &Developer for Java2",by Kathy Sierra and Bert Bates
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider this example


Thread t=new Thread(){
public void run()
{
System.out.println("run");

}
};
t.start();

here a subclass of Thread is created and assigned to the Thread super class

what i mean to say is that by default if u create an anonymous class it is a subclass of declared class
and according to k&b u cannot extend and implement at the same time

but here u can implement an interface and by default ur extending Thread class

hope that helps
 
shandilya popuru
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1.does an anonymous inner class have constructor?


no u cannot declare a constructor for an anonymous inner class
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shandilya popuru:

no u cannot declare a constructor for an anonymous inner class



but it does have one
ALL classes have a constructor, even if it is a default no-argument constructor.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Option D above says

"Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements a single interface "

It specifically mentions that Super (super class of the anonymous inner class) doesnt implement an interface. In this case how does the Thread example above hold good. Besides, going strictly by what has been said, is the statement D above correct ? I feel to the contrary - correct me if otherwise.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try to write code in which an anonymous inner class implements an interface not implemented by the class it's derived from.
There's no way to tell the compiler which interface that would be in a line like
(see below for full example).

In the case of the Thread example you're extending a class that implements an interface (Thread implements Runnable) so there is no problem.

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

D. Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements a single interface


Still i am not able to understand how is this true
Anyone please help?

Thanks
Ani
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think D is wrong as it says anonymous class "immediate Subclass of Super" meaning it had already been extended from Super class, How can it implement a interface?? It violates the Rule that anonymous class can't extend and implement at the same time.

Is n't that correct?
 
reply
    Bookmark Topic Watch Topic
  • New Topic