Skip to content

Override concrete method with abstract method #12383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kynthus opened this issue Apr 25, 2021 · 1 comment
Closed

Override concrete method with abstract method #12383

kynthus opened this issue Apr 25, 2021 · 1 comment

Comments

@kynthus
Copy link

kynthus commented Apr 25, 2021

reproduction steps

using Scala 2.13.5, on Oracle Java SE Development Kit 8u202.

In the #6760.
Scala does not allow non-public concrete methods in superclasses to be made public when declaring them as abstract in subclasses.

// BaseJava.java

public abstract class BaseJava {
    protected          void fn1() { System.out.println("BaseJava#fn1()"); }
    protected          void fn2() { System.out.println("BaseJava#fn2()"); }
    protected abstract void fn3();
}
// DerivedScala.scala

abstract class DerivedScala extends BaseJava {
  override           def fn1(): Unit // error
  override protected def fn2(): Unit // ok
  override           def fn3(): Unit // ok
}

question

Is this specification correct? Java allows this.

// DerivedJava.java

public abstract class DerivedJava extends BaseJava {
    @Override public    abstract void fn1(); // ok
    @Override protected abstract void fn2(); // ok
    @Override public    abstract void fn3(); // ok
}

If protected is added(fn2) or the abstract method is redefined(fn3), Scala will not cause an error.

@som-snytt
Copy link

The specification is correct by definition. As the linked ticket says, concrete overrides abstract, irrespective of linearization.

Scala 3 says

-- Error: DerivedScala.scala:2:15 ----------------------------------------------
2 |abstract class DerivedScala extends BaseJava {
  |               ^
  |error overriding method fn1 in class DerivedScala of type (): Unit;
  |  method fn1 in class BaseJava of type (): Unit has weaker access privileges; it should be public;
  |  (Note that method fn1 in class DerivedScala of type (): Unit is abstract,
  |  and is therefore overridden by concrete method fn1 in class BaseJava of type (): Unit)
1 error found

I thought they intended to modify this rule, so it's worth expressing your incredulity on gitter or the forum. They have tweaked interop rules similarly to what is suggested on the ticket, that is, a special rule when a Java definition is implicated.

One could also lodge a ticket against Scala 3 for their exuberant use of semicolon in the error message. And lack of period. (See what I did there.)

Not sure whether to close this is as duplicate, question (see "please, no questions"), or both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants