score:2
the constructor is not any utility function, it is a function that should guarantees that object is fully initialized after it is run.
so if you call car
constructor it will return fully initialized car
in the process your instance should become fully initialized learninheritance
before you start initializing it as car
. so you should assume that you can call only one parent constructor.
to deal with this restriction you should require that there is some parent constructor that does all the job, where all other set up some defaults. then you could call that constructor yourself with your constructor that does all the job, and have other constructors act as utilities with defaults.
class foo(name: string, age: int) {
def this(name: string) = this(name, 10)
def this(age: int) = this("", age)
}
class bar(name: string, age: int) extends foo(name, age) {
def this... // bar constructors
}
if you cannot do this, then unfortunately you have to pick only one case in your class.
alternatively, consider that each case is a separate class:
class foo private (name: string, age: int) {
def this(name: string) = this(name, 10)
def this(age: int) = this("", age)
}
sealed trait bar { this: foo =>
// bar methods
}
object bar {
final class name(name: string) extends foo(name) with bar
final class age(age: int) extends foo(age) with bar
def apply(name: string): bar = new name(name)
def apply(age: string): bar = new age(age)
}
but this has its own issues.
most of the time, though, usage of only one parent constructor for everything is not a problem. if it is then we can think for a solution for a particular case.
score:2
constructors are not instance methods. that means they are not inherited.
Source: stackoverflow.com
Related Query
- Does extending a class in scala inherits auxilary constuctor also?
- Does extending a class in scala with constructor params add vals (fields) to the class?
- Extending scala case class without constantly duplicating constructors vals?
- Scala case class extending Product with Serializable
- What does => mean at the beginning of a Scala class definition?
- Why does Scala place a dollar sign at the end of class names?
- Scala Extending Parameterized Abstract Class
- What does [+A] mean in Scala class declaration?
- Extending Java Class with Scala Trait
- Why does scala allow private case class fields?
- How to read the class of a Scala object extending Any but not AnyRef?
- Scala - Injecting Generic type using guice when dependency class is also using same generic type
- Implementing a string class that does case insensitive comparisions in Scala
- Why does running javap on a compiled Scala class show weird entries in the constant pool?
- Renaming a .scala file in Scala IDE does not rename the class
- In a Scala class declaration, does the "with" keyword specify an "is-a" relationship?
- Why does Scala complain about illegal inheritance when there are raw types in the class hierarchy?
- Scala Wrapper class by extending Component and with the SequentialContainer.Wrapper trait, do I have the correct understanding of traits?
- Scala extending the List class with a coalesce preappend method
- Extending a scala class with less ceremony
- what does this extra private[class]() in scala class definition mean?
- Why does Scala 2.9.1 not realize NodeSeq is also GenTraversableOnce
- Extending a Java class in Scala while keeping the field accessible
- Scala Why does the List class define a toList Method?
- Extending a class with many implementation that each have main methods in Scala
- Scala Generics with case class extending sealed trait
- Does Scala support inner class generic
- Why does html constructed within play 2 scala templates produce a empty Html case class
- Equality of Scala case class does not work in junit assertEquals when it contains an inner Array
- How does type class resolution in scala work?
More Query from same tag
- (How) Can you curry compose monadic functions?
- Scala always returning true....WHY?
- Http Akka route with regular expression is not compiling
- Can Scala case classes match in both an inherited and non-inherited function?
- How to do isolated transaction in Postgres with Scala Slick
- Confusing println output order
- Count Vowels in List of Strings
- imported object is permanently hidden warning by definition of object in Scala Spec
- shorter way to get an updated immutable object?
- Diffrence between "for (elm <- myList) yield f(_)" and "myList map f(_)" in Scala
- When should I use Scala's Array instead of one of the other collections?
- Why binding a variable to Exception types in a Scala pattern with pipes is inferred to type "Throwable"?
- Type upper bound in Scala Mapper/Record traits
- Why does the Scala compiler give "value registerKryoClasses is not a member of org.apache.spark.SparkConf" for Spark 1.4?
- difference between Split with a string and char argument in Scala?
- Is method parameter forwarding possible in Scala?
- How to create a small library with a "legacy" interface using ZIO (having persistent layers)
- How to input parameters when running bash command with Scala
- In Spark Scala, how to check how many characters in a string in a dataframe column are uppercase?
- efficient, but still precise, multiple of exponent
- How to transform a Map[Symbol, List[A]] to a List[Map[Symbol,A]] in Scala?
- Calling a constructor through java reflection in scala 2.10
- Akka actor throws null pointer error, reason unknown
- Nifi and Spark Integration
- Scala. Can case class with one field be a value class?
- Scala - combinator parser incorrect - repsep
- Spark Scala Cosine Similarity Matrix
- on demand actor get or else create
- ClassCastException in scala
- Play 2.6 I18N Twirl fails in production mode