Question

1    public interface Word { boolean isSpelled(String w); }
2    
3    abstract class Verb3 implements Word {
4       boolean isSpelled(String w) { return true;}
5    }

This is a question from OCJP mock test. The above code will return errors on line 4 and the answer said Verb3 class fails attempting to assign weaker access privileges to isSpelled() method.

Anyone knows what it means?

In addition, when you implement interfaces, shouldn't the access privilege depends on the methods that is declared in the interfaces? For instance,

1    public interface Word { public boolean isSpelled(String w); }

Using the public or default(no access modifier in this case) will let you access or override the method. Please correct me if I'm wrong. Thanks.

Was it helpful?

Solution

When you declare a method in an interface its public by default. But when you define it in any class its like if you dont specify the access modifier its default. While implementing an interface you cannot assign weaker access to the overridden method. When you implement an interface method it must be declared public.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top