aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes q on interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "q on interfaces" Watch "q on interfaces" New topic
Author

q on interfaces

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
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.


SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75

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.


thanks & regards,<br />Sanjeev.
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
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 ]
Federico Zuppa
Greenhorn

Joined: Nov 23, 2004
Posts: 7
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.
prajkta patil
Ranch Hand

Joined: Nov 13, 2004
Posts: 49
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

Joined: Nov 13, 2004
Posts: 49
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

Joined: Nov 13, 2004
Posts: 49
sorry A & E will not compile.by mistake i have written it.
prajkta patil
Ranch Hand

Joined: Nov 13, 2004
Posts: 49
sorry again i have written it in hurry.
only B & E will compile..
Federico Zuppa
Greenhorn

Joined: Nov 23, 2004
Posts: 7
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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: q on interfaces