aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface qustion 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 "Interface qustion" Watch "Interface qustion" New topic
Author

Interface qustion

guo mark
Ranch Hand

Joined: Oct 20, 2001
Posts: 43
Hi every one in javarach
i got a question like that
nterface Foo{
int k=0;
}
public class Test implements Foo{
public static void main(String args[]){
int i;
Test test = new Test();
i=test.k;
i=Test.k;
i=Foo.k;
}
}
A.Compilation succeeds.
B.An error at line 2 causes compilation to fail.
C.An error at line 9 causes compilation to fail.
D.An error at line 10 causes compilation to fail.
E.An error at line 11 causes compilation to fail.

the given answer is a .
I checked a by running the program,is just ok.Its seemed that K in interface Foo has been extended by the class and was made someting like static value.Its ture ?


meet once ,love every while
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Its seemed that K in interface Foo has been extended by the class and was made someting like static value.Its ture ?
Just a nitpick on the terminology you are using. Class Test does not extend interface Foo.
Class Test implements interface Foo.
Moreover, all member variables declared in an interface are implicitely public, static and final, and thus, k is a static member of interface Foo and any class that implements it.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
guo mark
Ranch Hand

Joined: Oct 20, 2001
Posts: 43
Thanks Valentin Crettaz. I use "extend" instead of "implement" only because k is a value and i am not sure that may be a value can be implementsed or only be extended.
Thanks again ,i really get you means.
[ March 20, 2002: Message edited by: guo mark ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Class Test does not implement or extend a "value", it implement one or more interfaces or extend a class. A class further inherits the non-private members (variables, methods) of the class it extends or interface(s) it implements.
guo mark
Ranch Hand

Joined: Oct 20, 2001
Posts: 43
ok,then i have a further question.
in my opinion, interface only should to declare what to implement but not how to .its that so ,what are the diffrerents between abstract class and interface excepte that interface can be muti-inherited?
[ March 20, 2002: Message edited by: guo mark ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The following discussions handle that question:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015151
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015080
guo mark
Ranch Hand

Joined: Oct 20, 2001
Posts: 43
Thanks again.people in javaranch are aways so nice. .
BTW,you picture is really cool.
[ March 20, 2002: Message edited by: guo mark ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
you picture is really cool
you must be blind then
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface qustion