admin管理员组文章数量:1356575
The scala project is depending on the Java lib that I cannot modify. I need to extend one of the Java classes and override method, however I have issues with protected
visibility:
package com.a;
class Foo {
protected Baz protectedBaz;
Foo(Int i) { ... }
Foo(String s) { ... }
protected void init() { ... }
}
package com.b;
class BarFoo extends Foo {
BarFoo(Int i) { ... }
BarFoo(String s) { ... }
}
Now, In scala I need to extend BarFoo
, override the init
method and modify the protectedBaz
:
override def init(): Unit = {
super.init() // I need BarFoo to do its work
this.protectedBaz.setA(..)
}
End of problem statement
Attempt
Before I even get there, I need to deal with the constructor problem. I followed the solution here:
object CustomBarFoo {
def apply(i: Int) = new BarFoo(i) with CustomBarFoo
def apply(s: String) = new BarFoo(s) with CustomBarFoo
}
trait CustomBarFoo extends BarFoo {
override def init(): Unit = {
super.init()
this.protectedBaz.setA(..) <<< Implementation restriction: trait CustomBarFoo accesses protected variable protectedBaz inside a concrete trait method.
}
}
The suggested solution here is to subclass and redefine visibility.
I have a classical chicken and egg problem. I cannot create a subclass class CustomBarFoo extends BarFoo
because of the constructor issues. But the only way to redefine the visibility is to subclass.
The scala project is depending on the Java lib that I cannot modify. I need to extend one of the Java classes and override method, however I have issues with protected
visibility:
package com.a;
class Foo {
protected Baz protectedBaz;
Foo(Int i) { ... }
Foo(String s) { ... }
protected void init() { ... }
}
package com.b;
class BarFoo extends Foo {
BarFoo(Int i) { ... }
BarFoo(String s) { ... }
}
Now, In scala I need to extend BarFoo
, override the init
method and modify the protectedBaz
:
override def init(): Unit = {
super.init() // I need BarFoo to do its work
this.protectedBaz.setA(..)
}
End of problem statement
Attempt
Before I even get there, I need to deal with the constructor problem. I followed the solution here:
object CustomBarFoo {
def apply(i: Int) = new BarFoo(i) with CustomBarFoo
def apply(s: String) = new BarFoo(s) with CustomBarFoo
}
trait CustomBarFoo extends BarFoo {
override def init(): Unit = {
super.init()
this.protectedBaz.setA(..) <<< Implementation restriction: trait CustomBarFoo accesses protected variable protectedBaz inside a concrete trait method.
}
}
The suggested solution here is to subclass and redefine visibility.
I have a classical chicken and egg problem. I cannot create a subclass class CustomBarFoo extends BarFoo
because of the constructor issues. But the only way to redefine the visibility is to subclass.
- What versions of Scala and Java are you using? – Dmytro Mitin Commented Mar 28 at 16:29
- It's scala 2.12 and java 8 – Hedrack Commented Mar 30 at 20:17
1 Answer
Reset to default 1In Scala 2.13.16 the full error message is
Implementation restriction: trait CustomBarFoo accesses protected variable protectedBaz inside a concrete trait method.
Add an accessor in a class extending class Foo as a workaround. <-- !!!
Even if you can't modify Java library, you still can define your own Java class providing a public accessor to the protected field:
public class SubBarFoo extends BarFoo {
public SubBarFoo(int i) {
super(i);
}
public SubBarFoo(String s) {
super(s);
}
public Baz getProtectedBaz() {
return protectedBaz;
}
}
And now you should be able to use this accessor in Scala:
object CustomBarFoo {
def apply(i: Int) = new SubBarFoo(i) with CustomBarFoo
def apply(s: String) = new SubBarFoo(s) with CustomBarFoo
}
trait CustomBarFoo extends SubBarFoo {
override def init(): Unit = {
super.init()
this.getProtectedBaz.setA()
}
}
One more option is to define CustomBarFoo
in your Scala code in the same package as Foo
in the Java library. Scala protected
members are accessible in inheritors but Java protected
members are accessible also in the same package.
本文标签: scalaSubclassing and overriding Java class with protected field accessStack Overflow
版权声明:本文标题:scala - Subclassing and overriding Java class with protected field access - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744057753a2583563.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论