2

In documentation says that readExternal() and writeExternal() have default access modifier and I also implement it with default access modifier, but somehow I have 2 error which says that I assign weaker access modifier? So why is that? (I know that using default modifier I can not serializa/deserialize objects from other package)

Error1:

Error:(19, 10) java: readExternal(java.io.ObjectInput) in A cannot implement readExternal(java.io.ObjectInput) in java.io.Externalizable attempting to assign weaker access privileges; was public

Error2:

Error:(16, 10) java: writeExternal(java.io.ObjectOutput) in A cannot implement writeExternal(java.io.ObjectOutput) in java.io.Externalizable attempting to assign weaker access privileges; was public

class A implements Externalizable
{
    public A() {}
    
    @Override
    void writeExternal(ObjectOutput out) throws IOException {}
    
    @Override
    void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {}
}
Denys_newbie
  • 1,140
  • 5
  • 15
  • Maybe because you have to declare methods as public? The main point is when you implement/extend some interface/class you have to use at least the same access modifier or wider. – Николай Гольцев Jan 20 '21 at 19:26
  • But I used the same access modifier: I have package-private and in documentation also package-private – Denys_newbie Jan 20 '21 at 19:33
  • 3
    Interface methods are always public. See https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4, in particular, "Every method declaration in the body of an interface is implicitly `public`". The `public` modifier is omitted from the documentation of interface methods because it is redundant. – Luke Woodward Jan 20 '21 at 19:57
  • @LukeWoodward you are right, maybe post it as an answer? – Denys_newbie Jan 20 '21 at 20:02
  • This question is quite probably a duplicate of some other questions on StackOverflow, such as https://stackoverflow.com/questions/5376970/protected-in-interfaces . – Luke Woodward Jan 20 '21 at 20:06

0 Answers0