Results 1 to 9 of 9
Thread: Interface problem
- 02-23-2011, 05:15 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
- 02-23-2011, 05:19 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
I know we do that as public but what is the rason behind it
What error message does the compiler give you when you try and compile a class implementing an interface that does not declare the methods it is implementing as public?
- 02-23-2011, 05:27 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
This is the program:
interface Interface {
void callback(int param);
}
class Client implements Interface{
void callback(int p){
System.out.println("Interface example");
}
}
javac Client.java
compiler message:
Client.java:5: callback(int) in Client cannot implement callback(int) in Interface; attempting to assign weaker access privileges; was public
void callback(int p){
^
1 error
- 02-23-2011, 05:39 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
The compiler is telling you two things which, together, might help to explain why you have to use "public".
(1) "was public" at the end means that the declaration of callback() in the interface was implicitly public. That is, it is optional whether we write "public" or not in the interface but the method we are declaring is always public.
(2) "attempting to assign weaker access privileges" is getting at the fact that in the class callback() is declared with default access. Default access is weaker in the sense that fewer classes can access a default access method than can access a public one.
The reason why you can't assign weaker access privileges in a class that is overriding a method is that the original interface was making a promise: "callback() is public - I promise all the world that you may call it". But in the implementing class you are going back on that promise and saying "only some classes may access callback()". Not breaking promises is fundemental to what interfaces are all about.
(As a minor point you might wonder why, since Client implements Interface, the silly compiler doesn't look at the methods that Interface declares and allow you the same ability to have them public without having to say that explicitly. There's probably no big reason for that - it probably makes less work for the compiler and it certainly makes the code easier to read: if you just saw the Client class code on its own you have a hard job trying to figure out that callback() was public without that keyword being used.)Last edited by pbrockway2; 02-23-2011 at 07:35 AM.
- 02-23-2011, 06:59 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
pbrockway2 is right
while implementing interface method in subclass it cannot have a weaker access privileges
- 02-23-2011, 08:11 AM #6
The methods inside the interface are implicitly public and abstract both. But it's not mandatory to mention that in the code.
So, the interface declared as,
is seen by the compiler as,Java Code:public interface Interface { void method1(); void method2(); }
Java Code:public interface Interface { public abstract void method1(); public abstract void method2(); }
Hope that makes it more clear,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-23-2011, 08:21 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Personally I find it a bit sloppy, language wise; i.e. an interface can have package scope (don't mention it as public or whatever) but still the methods are implicitly public; why not give the methods the same visibility as the interface itself? An implementation can still widen the visibility if it wants (narrowing is never allowed). I'm open for counter examples that show it can't be done ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-23-2011, 08:35 AM #8
What I meant was the compiler will anyways see the methods as public abstract. So it's fine even if you don't mention that [considering you know their real signature i.e. public abstract]
But yes, I agree with you that it should be mentioned at the interface level itself, to avoid anymore confusion while implementation by other classes.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-23-2011, 09:57 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Java user interface problem
By eevaa in forum New To JavaReplies: 4Last Post: 05-23-2010, 02:52 PM -
Annoying problem class/interface error
By Chick786 in forum New To JavaReplies: 2Last Post: 04-11-2010, 04:36 PM -
Problem With RSA Interface
By Floetic in forum AWT / SwingReplies: 2Last Post: 03-25-2008, 10:31 AM -
interface Comparable<T> problem
By Lennon-Guru in forum New To JavaReplies: 3Last Post: 03-05-2008, 12:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks