• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

From Khalid's Book (Question on interfaces)

 
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
 
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
 
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.
 
mister krabs
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
 
Do you pee on your compost? Does this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!