SlideShare a Scribd company logo
1 of 9
OOP in Scala
Encapsulation, Inheritance, Polymorphism, abstractions and
variances
in comparison to C#
Agenda
• Intro to Scala as programming language
• Encapsulation
• Abstractions
• Inheritance
• Polymorphism
• Variance
• References
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)
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;}
}
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.”
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
}
}
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
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>
References
• http://docs.scala-lang.org
• https://twitter.github.io/scala_school/
• http://blog.jaceklaskowski.pl/2015/05/15/ad-hoc-polymorphism-in-scala-
with-type-classes.html
• http://www.scala-js.org

More Related Content

What's hot

楽々Scalaプログラミング
楽々Scalaプログラミング楽々Scalaプログラミング
楽々Scalaプログラミング
Tomoharu ASAMI
 

What's hot (20)

Typescript
TypescriptTypescript
Typescript
 
Dmytro Kochergin Angular 2 and New Java Script Technologies
Dmytro Kochergin Angular 2 and New Java Script TechnologiesDmytro Kochergin Angular 2 and New Java Script Technologies
Dmytro Kochergin Angular 2 and New Java Script Technologies
 
Js training
Js trainingJs training
Js training
 
Scala basic
Scala basicScala basic
Scala basic
 
SacalaZa #1
SacalaZa #1SacalaZa #1
SacalaZa #1
 
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
 
楽々Scalaプログラミング
楽々Scalaプログラミング楽々Scalaプログラミング
楽々Scalaプログラミング
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
 
AngularConf2015
AngularConf2015AngularConf2015
AngularConf2015
 
Introducing TypeScript
Introducing TypeScriptIntroducing TypeScript
Introducing TypeScript
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
 
Introduction about type script
Introduction about type scriptIntroduction about type script
Introduction about type script
 
Java session 3
Java session 3Java session 3
Java session 3
 
A Deep Dive into Javascript
A Deep Dive into JavascriptA Deep Dive into Javascript
A Deep Dive into Javascript
 
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
 
Introduction to php 4
Introduction to php   4Introduction to php   4
Introduction to php 4
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
 
TypeScript 101
TypeScript 101TypeScript 101
TypeScript 101
 
Scala for C# Developers
Scala for C# DevelopersScala for C# Developers
Scala for C# Developers
 
Swift Micro-services and AWS Technologies
Swift Micro-services and AWS TechnologiesSwift Micro-services and AWS Technologies
Swift Micro-services and AWS Technologies
 

Similar to OOP in Scala

Rafael Bagmanov «Scala in a wild enterprise»
Rafael Bagmanov «Scala in a wild enterprise»Rafael Bagmanov «Scala in a wild enterprise»
Rafael Bagmanov «Scala in a wild enterprise»
e-Legion
 

Similar to OOP in Scala (20)

Aggregate Programming in Scala
Aggregate Programming in ScalaAggregate Programming in Scala
Aggregate Programming in Scala
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
 
JAVA INTRODUCTION
JAVA INTRODUCTIONJAVA INTRODUCTION
JAVA INTRODUCTION
 
JVM languages "flame wars"
JVM languages "flame wars"JVM languages "flame wars"
JVM languages "flame wars"
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?
 
Scala Introduction
Scala IntroductionScala Introduction
Scala Introduction
 
Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
Real-world polyglot programming on the JVM  - Ben Summers (ONEIS)Real-world polyglot programming on the JVM  - Ben Summers (ONEIS)
Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
 
Rafael Bagmanov «Scala in a wild enterprise»
Rafael Bagmanov «Scala in a wild enterprise»Rafael Bagmanov «Scala in a wild enterprise»
Rafael Bagmanov «Scala in a wild enterprise»
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
Scala in a nutshell
Scala in a nutshellScala in a nutshell
Scala in a nutshell
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologist
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
 
Building DSLs with Scala
Building DSLs with ScalaBuilding DSLs with Scala
Building DSLs with Scala
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian DragosScala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian Dragos
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
First class Variables in Pharo
First class Variables in PharoFirst class Variables in Pharo
First class Variables in Pharo
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Scala in a wild enterprise
Scala in a wild enterpriseScala in a wild enterprise
Scala in a wild enterprise
 
Scala on Android
Scala on AndroidScala on Android
Scala on Android
 

OOP in Scala

  • 1. OOP in Scala Encapsulation, Inheritance, Polymorphism, abstractions and variances in comparison to C#
  • 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>
  • 9. References • http://docs.scala-lang.org • https://twitter.github.io/scala_school/ • http://blog.jaceklaskowski.pl/2015/05/15/ad-hoc-polymorphism-in-scala- with-type-classes.html • http://www.scala-js.org

Editor's Notes

  1. 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).
  2. 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.
  3. 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.
  4. 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]?