3

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?

Jetbo
  • 95
  • 3
  • 6

1 Answers1

2

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
  }
}
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • 1
    You haven't answered the question properly. You appear to just have rewritten the question to be what you want it to be. The question assumes you can't change the original trait/object/function. You have thrown that assumption out the window and answered your own question. – hawkeye Jul 23 '18 at 11:59