Skip to content

week 4.3 4.4 polymorphysm

Alina GHERMAN edited this page Aug 16, 2016 · 1 revision

type of polymorphism ==> bounds

def assertAllPos[S<:IntSet](r:S):S

S is a subtype of IntSet

Notes:

  • In java Arrays are co-variant ==> we allow to have subtypes of the defined types
  • In scala List are covatiant
  • In scala Arrays are not ==> new Array[NonEmpty]
  • class is covariant C[+A]
  • methods are covariant for results, and are not covariant for parameters

Object vs class:

  • You can think of the object keyword as creating a singleton object of a class that is defined implicitly.

Abstract classes vs traits:

  • Abstract classes can have constructor parameters as well as type parameters. Traits can have only type parameters. There was some discussion that in future even traits can have constructor parameters
  • Abstract classes are fully interoperable with Java. You can call them from Java code without any wrappers. Traits are fully interoperable only if they do not contain any implementation code
Clone this wiki locally