Java Getting an Error for Implementing Interface Method with Weaker Access

doSomething() in ClassOne can't implement doSomething() in InterfaceOne, attempt to assign weaker access privilege, was public

Methods in interfaces are public. Methods without access modifier package-private, i.e. weaker access privilege. Add public modifier to doSomething in ClassOne

public class ClassOne {
public void doSomething(){
System.out.println("do something from InterfaceMethod class");
}
}

You can see the access modifiers table at the documentation

Javac fails with attempting to assign weaker access privileges on intersection type

JLS §4.9 defines intersection types and what are their members via the concept of a notional class. In your particular case the notional class is:

class <notional> extends FooSupport implements FooInterface {}

Please note the empty class body.

The JLS paragraph is meant to imply that the notional class must be well-formed, or a compile error occurs. Clearly a class that inherits a protected implementation for a public interface method is not well-formed.

I agree that there could be other ways of specifying an intersection type that would admit unknown subclasses of FooSupport that would resolve the above conflict by overriding the method with public visibility.

I believe the style of definition via the notional class was chosen to keep complexity at bay. See how succinct §4.9 is in comparison to other definitions.

PS: Also ecj rejects your example, saying:

The inherited method IntersectionBug.FooSupport.foo() cannot hide the public abstract method in IntersectionBug.FooInterface

java attempting to assign weaker access privilege error

The lines with the problem are the two below

new Wait("") {boolean until() {return false;}};session().open("/");
new Wait("") {boolean until() {return false;}};session().click("id=btnLogin-button");

You try to override the until method which has public access in the com.thoughtworks.selenium.Wait class by a until method which is only package visible.

You cannot override a method and reduce visibility. You can only increase the visibility (e.g. overriding a protected method and making it public)

So the fix would be to add the public keyword to these methods

new Wait("") {public boolean until() {return false;}};session().open("/");
new Wait("") {public boolean until() {return false;}};session().click("id=btnLogin-button");

Why the overridden method should be public in this program?

int divisor_sum(int n) implements an interface method. Interface methods have public access (even if you don't specify it explicitly), so you cannot reduce the visibility of the method in the implementing class.

Consider the following:

MyCalculator mc = new MyCalculator();
AdvancedArithmetic aa = mc;

If you don't give divisor_sum() method in MyCalculator class a public access level, you won't be able to call it via the class reference (mc.divisor_sum(4)), but you would be able to call it via the interface reference (aa.divisor_sum(4)). This makes no sense, and therefore not allowed.

Why can I not call the interface method?

For the original error in question, the clue was found on another error I hadn't noticed earlier on the chatButton.setOnClickListener.

The OnClickListener method onClick must be overridden like this:

chatButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
String referenceID = listingEntry.referenceID;
delegate.chat(listingEntry,null);
}
});

This fixes the resolution error at the call site.



Related Topics



Leave a reply



Submit