Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

From Khalid's Book (Question on interfaces)

 
Rahul Mehta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here goes the question :
what is wrong ,if anything with the folloeing code:
abstract class MyClass implements Interface1,Interface2{
void f(){};
void g(){};
}
interface Interface1{
int VAL_A = 1;
int VAL_B = 2;
void f();
void g();
}
interface Interface2{
int VAL_B = 3;
int VAL_C = 4;
void g();
void h();
}
According to the book , the code should compile without any errors but when I tried to compile it . It gave following error:
g() in MyClass cannot implement g() in Interface1; attempting to assign weaker access privileges; was public
Can anyone explain me the error.
TIA
Rahul
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ahaaa. That had happened to me a few times. It does not work until you change the access levels of the methods in MyClass to
public . Always intended to find out why. I'll poke around in the JLS and let you know if I come up with something.

cheers
Sahir
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nothing in JLS. Tried putting them in same package, different package , messed with the class path , using import, export.
No dice. Stumped ! Any ideas guys?
rgds
Sahir
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember that interfce methods are implicitly public, correct me if I'm wrong.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely! All interface methods are public by default and since you didn't specify anything in the class you were trying to redefine them as package. Bzzzt! Not permitted!
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aaah! So that was it. Thanks guys.
Rgds
Sahir
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic