PDA

Click to See Complete Forum and Search --> : Interface Errors(Weaker Access Privileges )


prasadkowli80
Mar 26th, 2004, 09:03 AM
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

Dillinger4
Mar 26th, 2004, 12:47 PM
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.

CornedBee
Mar 27th, 2004, 06:39 AM
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.