score:0
a first step to an answer to might look like this
class animalia {
type fundamentalcelltype = eucharyotic // all sub species inherit from this.
val hasdifferentiatedtissues: boolean
type phylumt <: animalia
val phylum : phylumt
type klasst <: phylum.type //not sure if this would work.
val klass: klasst
type genust <: klass.type //intermediate classification left out for simplicity
val genus: genust
}
score:1
you are confusing an inheritance hierarchy with the hierarchical biological classification of things! for example; why would chimpanzee
be an animalia
? why would a species be a phylum? these are distinct concepts.
what you are really looking for is aggregation (apologies for my lack of biological correctness)
trait animalia { def phyla: list[phylum] }
then we might have:
trait family { def phylum: phylum }
trait genus { def family: family }
trait species { def genus: genus }
and an instance of this:
object primates extends family { val phylum = mammals }
object apes extends genus { val family = primates }
object homosapiens extends species { val genus = apes }
score:2
oo inheritance modeling is simpler than it seems. the extends
keywords is a synonym for "is-a" relationship. so a "chimp is a primate and all primates are mammals" is meaningful so you can model this relationship as:
trait primate extends mammal
class chimp extends primate
but the relationship "a primate is an order" does not holds. so you cannot model it by inheritance. however, the "primate order is an order" is meaningful and you can model this relationship as:
object primates extends order
object mammals extends klass
you can then say that "chimp belongs to the primateorder", but that's a different relationship, that you can model as:
trait animal {
def order: order
def klass: klass
}
trait mammal extends animal {
val klass = mammals
}
trait primate extends mammal {
val order = primates
}
class chimp extends primate
chimp.order //=> primates
chimp.klass //=> mammals
Source: stackoverflow.com
Related Query
- Scala and OO: Specifying heirachy levels in super class
- Difference between object and class in Scala
- What is the difference between a class and a type in Scala (and Java)?
- Scala class and case class == comparison
- Generating a class from string and instantiating it in Scala 2.10
- Mockito matchers, scala value class and NullPointerException
- Scala 2.10, its impact on JSON libraries and case class validation/creation
- Scala class members and constructor parameters name clash
- Instantiate a Scala class from Java, and use the default parameters of the constructor
- Scala import statement at top and inside scala class
- Scala Error: Could not find or load main class in both Scala IDE and Eclipse
- Understanding Case class and Traits in Scala
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- How can I syntax check a Scala script without executing the script and generating any class files?
- Scala getters and setters in Java class
- scala class constructors and abstract types
- Scala wont pattern match with java.lang.String and Case Class
- Select certain super class for method call in Scala
- Scala getting field and type of field of a case class
- Difference between conversion with implicit function and implicit class in Scala
- Problem with bounded type parameterised case class and default args in Scala
- protected method that takes an abstract super class instance and the "access to protected method not permitted" error
- Scala Puzzle: enforcing that two function arguments are of the same type AND both are a subtype of a given class
- Scala -- class declared fields and access modifiers
- Implicit abstract class constructor parameter and inheritance in Scala
- Scala - how to create anonymous class and avoid hiding argument names
- Scala traits mixin order and super call
- How to call superclass constructor from child class in scala and how to do constructor chaining
- @JsonIgnore serialising Scala case class property using Jackon and Json4s
- Scala case class copy-method difference between 2.9 and 2.10
More Query from same tag
- How to create an instance of a generic type in a Scala 3 macro?
- spark error RDD type not found when creating RDD
- Scala Return type missmatch
- How can I make an actor class to observable in scala
- Spark scala dataframe for loop
- Not able to replace null values in Spark Dataframe with Integer value
- comparing against slick.lifted.Rep[java.sql.Timestamp]
- Spark - Drop null values from map column
- IntelliJ IDEA debug jumps inside instead of going over
- Iterate over a list, returning the current, next and the element before current
- java.sql.SQLException -> NumberFormatException when using .show() method on DataFrame in spark
- Check for unexpected exceptions using ScalaTest + ScalaCheck
- Apache Spark submit --files java.io.FileNotFoundException
- How to check whether key or value exist in Map?
- Scala: val foo = (arg: Type) => {...} vs. def(arg:Type) = {...}
- Drop every Nth element from a Scala Array
- Restrict Constructor Access
- Convert an Rows or Columns to a dataframe
- Functional Way of handling corner case in Folds
- Scala yield in case of success, but don't in case of exception
- Play Framework, JSON Overloaded method value [apply]
- How do you split a column such that first half becomes the column name and the second the column value in Scala Spark?
- Elastic search - scala case class @timestamp
- Shapeless Witness and how it can give the actual singleton type
- How to specify the return type of an abstract method to be the implementing class' type
- How to perform a join and/or cogroup operation on two maps in scala
- How to Test Kafka Consumer
- Extract value from Option of List
- Clustering in Akka
- Shapeless: map a tuple with some options to options