Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending class with parameter

I noticed that I can extend a class and at the same time pass parameters to the extended class:

scala> class SomeClass(val str: String)
defined class SomeClass

scala> class SubclassOne extends SomeClass("one")
defined class SubclassOne

scala> class SubclassTwo extends SomeClass("two")
defined class SubclassTwo

scala> val obj : SomeClass = new SubclassOne
obj: SomeClass = SubclassOne@2dca4eb4

scala> obj.str
res0: String = one

I'm working a lot with case classes, where ClassName(args) actually creates an object, so to me it looks like I'm extending an object, but I'm not sure here.

Does it perhaps mean that I'm extending the class, and automatically pass an argument to the super constructor?

like image 443
aioobe Avatar asked Jun 27 '26 17:06

aioobe


1 Answers

Does it perhaps mean that I'm extending the class, and automatically pass an argument to the super constructor?

Yes, that is exactly what it means.

like image 55
sepp2k Avatar answered Jun 29 '26 15:06

sepp2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!