Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In scala is it possible for a trait to extend a class which needs parameters?

I know a trait can extend a class which has an empty parameter constructor:

class Foo
trait Bar extends Foo

but is it possible to extend a class which constructor has some parameters?

class Foo(b: Boolean)
trait Bar extends Foo(true)

Is it possible to achieve this? It seems it's not possible. but why?

Thanks

like image 215
user1819676 Avatar asked Aug 21 '15 05:08

user1819676


1 Answers

Yes, it's possible, you just can't give constructor arguments:

trait Bar extends Foo { ... }

But to instantiate it you need to call the constructor as well:

new Foo(false) with Bar
like image 122
Alexey Romanov Avatar answered Oct 28 '22 01:10

Alexey Romanov