2. Agenda
• Intro to Scala as programming language
• Encapsulation
• Abstractions
• Inheritance
• Polymorphism
• Variance
• References
3. Short intro to Scala vs C#
Scala
• Compiles to JavaByteCode (but Scala.js compiles to
JavaScript)
• Hybrid programming language (strong typing,
imperative, declarative, functional, generic, object-oriented)
C#
• Compiles to MSIL
• Hybrid programming language (strong typing,
imperative, declarative, functional, generic, object-
oriented)
4. Encapsulation
• On Scala:
class Encapsulation(hidden: Any,
val readable: Any,
var settable: Any)
• Actually it transforms to next Java:
public class Encapsulation {
public java.lang.Object readable();
public java.lang.Object settable();
public void settable_$eq(java.lang.Object);
public
Encapsulation(java.lang.Object,
java.lang.Object, java.lang.Object) }
• On C#:
class Emcapsulation{
object hidden;
object readable;
public Emcapsulation(object hidden,
object readable,
object settable) {
hidden = hidden;
readable = readable;
Settable = settable;
}
public object Readable
{
get { return readable; }
}
public object Settable {get; set;}
}
5. Abstractions
• Class is an extensible program-code-template for creating objects, providing initial
values for state (member variables) and implementations of behavior.
• Object in Scala it's simply singleton. A class that can have only one
instance.
• Traits in Scala are best described as “interfaces that can provide concrete
members.”
6. Inheritance
Scala
• One base class (incl. abstract) and many traits
abstract class Window {
// abstract
def draw() }
class SimpleWindow extends Window {
def draw() { println("in SimpleWindow") // draw a basic
window }
C#
• One base class (incl. abstract) and many Interfaces
abstract class Window {
// abstract
public abstract void draw();
}
class SimpleWindow: Window {
public override void draw() {
Console.WriteLine("in SimpleWindow");
// draw a basic window
}
}
7. Polymorphism
• Parametric polymorphism (like Generics in C#)
• def head[A](xs: List[A]): A = xs(0)
• Subtype polymorphism
• def plus[A](a1: A, a2: A): A = ???
• trait Plus[A] {
def plus(a2: A): A
}
• def plus[A <: Plus[A]](a1: A, a2: A): A = a1.plus(a2)
• Ad-hoc polymorphism
• trait Plus[A] {
def plus(a1: A, a2: A): A
}
• def plus[A: Plus](a1: A, a2: A): A = implicitly[Plus[A]].plus(a1, a2)
Read more here: http://blog.jaceklaskowski.pl/2015/05/15/ad-hoc-polymorphism-in-scala-with-type-classes.html
8. Variance
• Variance annotations allow you to express the following relationships
between class hierarchies & polymorphic types:
Meaning Scala
notation
C# (only for
interfaces)
covariant C[T’] is a subclass of C[T] [+T] <outT>
contravariant C[T] is a subclass of C[T'] [-T] <inT>
invariant C[T] and C[T’] are not related [T] <T>
Getters and setters (or accessor/mutator methods) are used to encapsulate data, which is commonly considered one of the tenets of OOP.
They exist so that the underlying implementation of an object can change without compromising client code, as long as the interface contract remains unchanged.
This is a principle aiming to simplify maintenance and evolution of the codebase.
Even Scala has encapsulation, but it supports the Uniform Access Principle by avoiding explicit use of get/set (a JavaBean convention) by automatically creating accessor/mutator methods that mimics the attribute name (e.g. for a public val name attribute a corresponding def name public accessor is generated and for a var name you also have the def name_= mutator method).
Scala is more object-oriented than Java because in Scala we cannot have static members. Instead, Scala has singleton objects. A singleton is a class that can have only one instance, i.e., object. You create singleton using the keyword object instead of class keyword.
A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. Unlike class inheritance, in which each class must inherit from just one superclass, a class can mix in any number of traits.
Traits are used to define object types by specifying the signature of the supported methods. Scala also allows traits to be partially implemented but traits may not have constructor parameters.
In programming languages and type theory, polymorphism is the provision of a single interface to entities of different types.
The difference between subtyping and parametric polymorphism and ad hoc polymorphism is that the type hierarchy is expressed explicitly using extends keyword in Scala (for subtyping) or type parameters (for parametric polymorphism) while ad hoc polymorphism bases itself on implicit classes to mixin the behaviour (using traits). And that’s pretty much all, technically.
Parametric polymorphism refers to when the type of a value contains one or more (unconstrained) type variables,
so that the value may adopt any type that results from substituting those variables with concrete types.
ad hoc polymorphism is a kind of polymorphism in which polymorphic functions can be applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied.
Scala’s type system has to account for class hierarchies together with polymorphism. Class hierarchies allow the expression of subtype relationships. A central question that comes up when mixing OO with polymorphism is: if T’ is a subclass of T, is Container[T’] considered a subclass of Container[T]?