• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about interface

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following will NEVER compile correctly in any situation?
(Select three correct answers)
Assume TestInterface is a valid interface.
A: public TestInterface getInterface1( int i )
{
return new TestInterface ( )
{
{
System.out.println(i);
}
};
}
B: public TestInterface getInterface2( final int i )
{
return new TestInterface ( )
{
{
System.out.println(i);
}
};
}
C: public TestInterface getInterface3( )
{
return new TestInterface ( int i )
{
{
System.out.println(i);
}
};
}
D: public TestInterface getInterface4( )
{
return new TestInterface ( final int i )
{
{
System.out.println(i);
}
};
}
E: public TestInterface getInterface5( )
{
return new TestInterface ( )
{
{
System.out.println( i );
}
};
}

ans: a,c,d
explanation:Inner class can only access final variables of the enclosing method.
Interface can never have any constructor, but C D try to access the constructor
but what i m confused is that in b, which is correct answerreturn new TestInterface ( final int i){...} it does try to access the construncor as well,doesn't it?
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Weilliu,
Yes, b is coreect answer, and it doesn't access constructor...(see code piece marked as bold )
public TestInterface getInterface2( final int i ) {
return new TestInterface ( ) {
{
System.out.println(i);
}
};
}
That's all,
Jamal Hasanov
www.j-think.com
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
it doesn't call the constructor because in an anonymous class
new TestInterface() {..}
this is creating an anonymous class which implements the interface TestInterface or extends a class TestClass (assuming a superclass)and is not calling any constructor. hope this helps.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic