• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface qustion

 
guo mark
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
guo mark
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
 
guo mark
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you picture is really cool
you must be blind then
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic