Results 1 to 9 of 9
  1. #1
    mohitchouhan is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Interface problem

    when we implement some interface in its sub class then we have to define the method of sub class as "public" Why do we do that??
    I know we do that as public but what is the rason behind it....
    Thanks in advance

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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?

  3. #3
    mohitchouhan is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  5. #5
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    pbrockway2 is right
    while implementing interface method in subclass it cannot have a weaker access privileges

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by mohitchouhan View Post
    I know we do that as public but what is the rason behind it....
    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,

    Java Code:
    public interface Interface {
    void method1();
    void method2();
    }
    is seen by the compiler as,
    Java Code:
    public interface Interface {
    public abstract void method1();
    public abstract void method2();
    }

    Hope that makes it more clear,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by goldest View Post
    The methods inside the interface are implicitly public and abstract both. But it's not mandatory to mention that in the code.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    mohitchouhan is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Java user interface problem
    By eevaa in forum New To Java
    Replies: 4
    Last Post: 05-23-2010, 02:52 PM
  2. Annoying problem class/interface error
    By Chick786 in forum New To Java
    Replies: 2
    Last Post: 04-11-2010, 04:36 PM
  3. Problem With RSA Interface
    By Floetic in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2008, 10:31 AM
  4. interface Comparable<T> problem
    By Lennon-Guru in forum New To Java
    Replies: 3
    Last Post: 03-05-2008, 12:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •