Results 1 to 3 of 3

Thread: Interface Errors(Weaker Access Privileges )

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    75

    Interface Errors(Weaker Access Privileges )

    hello friends ,
    i have written a simple java program .See the code below :

    nterface A {
    void test();
    }


    class B implements A {

    void test() {
    System.out.println(" i am in B ");
    }
    }//end of class B

    class InterfaceDemo {
    public static void main(String args[]) {

    B ob = new B();
    ob.test();
    }
    } // end of class InterfaceDemo

    This program after compiling gives the following error :

    " test() in B cannot implement test() in A; attempting to assign weaker access privileges; was public "

    i know the solution is : --> "In the implementation, explicitly declare the implementing method as public"
    But i dont know the reason behind it .......i want to know
    " the method should be declared public "??????
    Can please anybody clearly explain me in details ??
    Thanx in advance ,
    Prasad

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Method prototypes contained within interfaces are considered to be public regardless of whether the public modifer is specified.
    Same holds true for constants defined within interfaces which are public static and final.

    I am pretty sure the same rules that apply to overriding methods from a superclass apply to providing implementation for an interface method. The scope cannot be narrowed only widened.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes, you need to make test public in the implementing class.

    Interface methods are always public, or they fail to compile if you try something else. Which is stupid IMO, sometimes I'd love to have protected or package methods in an interface.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width