Jetbo
Jetbo

Reputation: 95

Override a function inside an Object inside of a Trait

Say I have something like this:

trait A {
  object B {
    def doSomething = "test"
  }
}

class C extends A {
   def out = print(B.doSomething)
}

class D extends A {
   // override B.doSomething
}

How do I override the function doSomething that is inside of object B?

Upvotes: 3

Views: 6936

Answers (1)

Justin Pihony
Justin Pihony

Reputation: 67065

This is kind of a duplicate, but of two separate issues:

First, objects are not meant to be overridden. Second, inheriting from a nested class is somewhat straightforward

class A{
  class B{
    def foo = 1
  }
}

class C extends A{
  class B extends super.B{
    override def foo = 2
  }
}

Upvotes: 2

Related Questions