• 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

q on interfaces

 
Ranch Hand
Posts: 303
  • 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 NOT compile correctly?
(Select three correct answers)
--------------------------------------------------------------------------------

Assume TestInterface is a valid interface.

--------------------------------------------------------------------------------

A:

public TestInterface getInterface1( int i )
{
return new TestInterface ( )
{
{
System.out.println(i);
}
};
}

interface TestInterface{ }

B:

public TestInterface getInterface2( final int i )
{
return new TestInterface ( )
{
{
System.out.println(i);
}
};
}

interface TestInterface{ }

C:

public TestInterface getInterface3( )
{
return new TestInterface ( int i )
{
{
System.out.println(i);
}
};
}

interface TestInterface{ int i = 0; }

D:

public TestInterface getInterface4( )
{
return new TestInterface ( final int i )
{
{
System.out.println(i);
}
};
}

interface TestInterface{ int i = 0; }

E:

public TestInterface getInterface5( )
{
return new TestInterface ( )
{
{
System.out.println( i );
}
};
}

interface TestInterface{ int i = 0; }

I could nt answer this question. could someone plz explain.
 
Ranch Hand
Posts: 75
  • 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 NOT compile correctly?
(Select three correct answers)
--------------------------------------------------------------------------------

Assume TestInterface is a valid interface.

--------------------------------------------------------------------------------




I could nt answer this question. could someone plz explain.



Jayashree, it's the same code which you have posted.
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A,C,E will not compile.
non-static anonymous inner class can access instance var. & local final variables of enclosing.
[ November 23, 2004: Message edited by: sanjeevmehra mehra ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A:inner classes can only refer to final instance variables and final methods of the enclosing class so A won't compile.
B: compiles
C,D: I have seen in Bruce Eckel's books that you can initialize a constructor from the superclass passing the variable directly (but not define a parameter here..)
E: compiles

Testing the different possibilities, I tried the following:

interface TestInterface{int i = 0;}

public class test9 {
public TestInterface getInterface3(final int i ){
return new TestInterface () {
{
System.out.println(i);
}
};
}

public static void main(String args[]) {
test9 t = new test9();
TestInterface g = t.getInterface3(9);
}

}

this prints: 0 (instead of printing what is been given in the parameter, it prints what is in the interface). I thought that the interface value would be hidden by the parameter value, but is not like that.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Federico,
what u have written is correct,A & E will compile but what code u have written is somewhat wrong.
in the methods of local class u can't use the same variable name as final(i.e. u have written getinterface( final int i & same i variable u have used in Testinterface ) bcause i run ur program & it gave me error.
But when i declared final variable as j then it didn't give me error.
local(inner) class can acess all the fields of outer class not ncessary only final but the methods of a local class can refer only to local variables that r declared final.
 
prajkta patil
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
federico,
u can read today's question of the day,krishna has given its link.its on interface.
it has given one property:
Interface variables has to be defined at the time of declaration, because by default final variables.Once assigned the value cannot be changed.
 
prajkta patil
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry A & E will not compile.by mistake i have written it.
 
prajkta patil
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry again i have written it in hurry.
only B & E will compile..
 
Federico Zuppa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I understand now. A local inner class can only access final local variables (final automatic variables) of the enclosing method but can access any variable of the enclosing class.
About the question on the value of i (last paragraph) it seems more like a question of scope as the variable i defined in the interface is not the same as the one defined in the parameter's list. (Am I right ?)
Thanks!!
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic