• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Interface qustion

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you picture is really cool
you must be blind then
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic