• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error in java code: m() in C cannot implement m() in A  RSS feed

 
Gabriele Amoroso
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain me why this code does not works?



When I try to compile it, I get this error message:
C.java:3: error: m() in C cannot implement m() in A
void m() {
^
attempting to assign weaker access privileges; was public

I do not undestand why this happens.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface members are implicitly public. Your method m() in C has default access (package-private), which is weaker.

Interface method implementations in implementing classes can never be private, protected or default (package-private). They have to be declared public.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Gabriele Amoroso
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much!!
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As all method of interface are public
and you will have to take care of it while implementing it.
In your case you are giving package-private access to your method
which is weaker access as compaired to public(so it is not allowed)

Reason for making method of interface public is:Interface was designed to allow object from different packages (this is basis of j2ee design)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!