3

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?

4
  • Don't extend case classes. There's some sort of bug in case class inheritance based on how Scala optimizes them for pattern matching. Commented Mar 21, 2011 at 17:33
  • But that's precisely what they do in the Tour of Scala: Case Classes? Commented Mar 21, 2011 at 19:09
  • It is fine for a case class to have a super class or traits. What you should avoid is having a case class as a super class. I.e. class Foo extends SomeCaseClass Commented Mar 22, 2011 at 13:01
  • Aha, ok, I see. Well I'm not doing that in this case anyway :) Commented Mar 22, 2011 at 13:15

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.