26
votes

I have a Java class that I have ported to Scala, but which still has some Java subclasses implementing abstract functionality.

The original Java had

public abstract class Base {
    ...
    protected abstract Foo getFoo();
}

which was extended

public class Fred extends Base {
    ...
    protected Foo getFoo() {
        return foo;
    }
}

Now when I port Base to Scala

class Base {
    ...
    protected def getFoo(): Foo
}

The compiler complains that

Cannot reduce the visibility of the inherited method ...


Now I understand that because its rules are subtly different, Scala tends to enforce visibility in the compiler rather than the bytecode, so that getFoo is marked public in the javap'd class. But

protected[packagename] def getFoo()

still does not generate a pukka (Java) protected method, although I think that the visibility should be the same.

Is there any way to persuade the Scala compiler to emit a method that Java will consider protected?

1
I'll accept an authoritative 'No'Duncan McGregor
Excellent question. I followed the exact same path before reaching this page.ebruchez

1 Answers

2
votes

If you can sandwich Scala between two Java implementations, you could create a Java superclass that only has the desired abstract protected methods, at least until you convert all subclasses to Scala (if this is actually code under your control). But I do hope there's a better way...

EDIT: Another option is to use some kind of bytecode rewriter, like the annotation processing tool.