Passionate developer in Java and Scala. And sometimes, something else. A few months per year, someone calls me "professor". CoFounder of Scala By The Lagoon @scalagoon
That's a yes. Because it's the same instance. super() does not target another instance, it just sets up the part of your instance that is defined in the superclass.
Your sc variable is not two separate instances, one of SuperClass and one from SubClass: it is just one instance of SubClass, where a part of the state is made from all SuperClass defines. You must call super() as first thing in the subclass constructor (your code won't compile otherwise) because you must first initialize the part the instance inherits, and only after that you can go on initializing the part that the subclass defines.
That's a yes. Because it's the same instance.
super()
does not target another instance, it just sets up the part of your instance that is defined in the superclass.Your
sc
variable is not two separate instances, one ofSuperClass
and one fromSubClass
: it is just one instance ofSubClass
, where a part of the state is made from allSuperClass
defines. You must callsuper()
as first thing in the subclass constructor (your code won't compile otherwise) because you must first initialize the part the instance inherits, and only after that you can go on initializing the part that the subclass defines.Is this clearer?
Yes. Thank you.
Though, final question, will it not compile if I don't call
super()
at all?