You are on page 1of 27

Object-Oriented Programming in

Scala

Dr. Nguyen Hua Phung

HCMC University of Technology, Viet Nam

08, 2016

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 1 / 29


Outline

1 OOP Introduction

2 Scala

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 2 / 29


Object-Oriented Programming Goals

Programs as collections of collaborating objects


Object has public interface, hidden implementation
Objects are classfied according to their behavior
Objects may represent real-world entities or entities
that produce services for a program
Objects may be reusable across programs

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 4 / 29


Introduction To OOP Languages

There are many object-oriented programming (OOP)


languages
Some are pure OOP language (e.g., Smalltalk).
Newer languages do not support other paradigms but
use their imperative structures (e.g., Java and C]).
Some support procedural and data-oriented
programming (e.g., Ada and C++).
Some support functional program (e.g., Scala)

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 5 / 29


Some Important Features of OOP

Abstract data types


Encapsulation
Information Hiding
Class Hierarchy
Inheritance
Polymorphism - Dynamic Binding

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 6 / 29


Data Abstraction

An abstract data type is data type that satisfies the


following two conditions:
Encapsulation:
Attributes and operations are combined in a single
syntactic unit
compiled separately
Information Hiding:
The access to members are controlled
Reliability increased

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 7 / 29


Information Hiding

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 8 / 29


ADT Example

Operations on a stack: Client code:


create(stack) ...
destroy(stack) create ( stk1 ) ;
push ( stk1 , c o l o r 1 ) ;
empty(stack) push ( stk1 , c o l o r 2 ) ;
push(stack,element) i f ( ! empty ( s t k 1 ) )
temp = t o p ( s t k 1 ) ;
pop(stack)
...
top(stack)

Implementation can be adjacent or linked list. Client: don’t


care!

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 9 / 29


Class Hierarchy

A class may have some Subclasses


A class may have one/many Superclass(es)

Animal

Fish Mammal WingedAnimal

Whale Dog Cat Bat

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 10 / 29


Inheritance

A subclass is able to inherit non-private members of


its superclasses
A subclass can add its own members and override
its inherited members
Single vs. multiple inheritance
Inheritance increases the reusability in OOP

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 11 / 29


Diamond Problem in Multiple Inheritance

B C

if D inherits from B and C different versions of the


same behaviour, which version will be effective in D?
this problem, called diamond problem, is solved
differently in different OO language
is there the diamond problem in Java?

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 12 / 29


Class vs. Object; Method vs. Message

Class vs. Object


A class defines the abstract characteristics of a thing
its attributes or properties and
its behaviors or methods or features.
A Dog has fur and is able to bark
An object is a particular instance of a class.
Lassie is a dog

Method vs. Message


A method describes a behavior of an object.
A dog can bark
A message is a process at which a method of an
object is invoked.
Lassie barks

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 13 / 29


Polymorphism

Polymorphism: different objects can respond to the


same message in different ways.
x,y
Point draw()

radius
Circle draw()

p.draw()
Dynamic binding

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 14 / 29


Other Concepts

There are two kinds of variables in a class:


Class variables
Instance variables
There are two kinds of methods in a class:
Class methods – accept messages to the class
Instance methods – accept messages to objects

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 15 / 29


Scala

Invented by Martin Ordesky at EPFL, Lausanne,


Switzerland.
Similar to Java
Work smoothly with Java
Run on Java Virtual Machine
OOP + FP
Include lexer and parser generator

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 17 / 29


Classes and Objects in Scala

Class
class [1]
abstract class [5]
trait [2]
case class [3]

Object
new <class name>
<case class name>
object

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 18 / 29


Example [7]

class R a t i o n a l ( n : I n t , d : I n t ) {
r e q u i r e ( d != 0)

p r i v a t e v a l g = gcd ( n . abs , d . abs )


p r i v a t e def gcd ( a : I n t , b : I n t ) : I n t =
i f ( b == 0 ) a else gcd ( b , a % b )
v a l numer = n / g
v a l denom = d / g

def t h i s ( n : I n t ) = t h i s ( n , 1 )

def + ( t h a t : R a t i o n a l ) : R a t i o n a l =
new R a t i o n a l (
numer ∗ t h a t . denom + t h a t . numer ∗ denom ,
denom ∗ t h a t . denom
)

override def t o S t r i n g = numer + " / " + denom


}
Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 19 / 29
Example on Abstract class [4]

a b s t r a c t class Element {
def c o n t e n t s : A r r a y [ S t r i n g ] / / no body : a b s t r a c t
val height = contents . length
val width =
i f ( h e i g h t == 0 ) 0 else c o n t e n t s ( 0 ) . l e n g t h
}
class ArrayElement ( c o n t s : A r r a y [ S t r i n g ] )
extends Element {
def c o n t e n t s : A r r a y [ S t r i n g ] = c o n t s
}
class LineElement ( s : S t r i n g )
extends ArrayElement ( A r r a y ( s ) ) {
override def w i d t h = s . l e n g t h
override def h e i g h t = 1
}

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 20 / 29


Example on Object [1]

object Element {

def elem ( c o n t e n t s : A r r a y [ S t r i n g ] ) : Element =


new ArrayElement ( c o n t e n t s )

def elem ( l i n e : S t r i n g ) : Element =


new LineElement ( l i n e )
}

v a l space = Element . elem ( " " )


v a l h e l l o = Element . elem ( A r r a y ( " h e l l o " , " w o r l d " ) )

Which kind of Element will be assigned to space and


hello?

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 21 / 29


Example on Case Class [3]

a b s t r a c t class Expr

case class Var ( name : S t r i n g ) extends Expr


case class Number (num : Double ) extends Expr
case class UnOp( o p e r a t o r : S t r i n g , arg : Expr )
extends Expr
case class BinOp ( o p e r a t o r : S t r i n g ,
l e f t : Expr , r i g h t : Expr ) extends Expr

v a l v = Var ( " x " )


v a l op = BinOp ( " + " , Number ( 1 ) , v )
v . name
op . l e f t

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 22 / 29


Example 1 on Traits [9]
Reusability:
a b s t r a c t class B i r d

t r a i t Flying {
def f l y M e s s : S t r i n g
def f l y ( ) = p r i n t l n ( f l y M e s s )
}
t r a i t Swimming {
def swim ( ) = p r i n t l n ( " I ’m swimming " )
}

class Penguin extends B i r d with Swimming

class Hawk extends B i r d with Swimming with F l y i n g {


v a l f l y M e s s = " I ’m a good f l y e r "
}
class F r i g a t e b i r d extends B i r d with F l y i n g {
v a l f l y M e s s = " I ’m an e x c e l l e n t f l y e r "
}

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 23 / 29


Example 2 on Traits [2]

Stackable Modifications:
a b s t r a c t class IntQueue {
def g e t ( ) : I n t
def p u t ( x : I n t ) }
class BasicIntQueue extends IntQueue {
p r i v a t e v a l b u f = new A r r a y B u f f e r [ I n t ]
def g e t ( ) = b u f . remove ( 0 )
def p u t ( x : I n t ) { b u f += x } }
t r a i t Doubling extends IntQueue {
a b s t r a c t override def p u t ( x : I n t ) { super . p u t ( 2 ∗ x ) } }
t r a i t I n c r e m e n t i n g extends IntQueue {
a b s t r a c t override def p u t ( x : I n t ) { super . p u t ( x + 1 ) } }

v a l queue =
new BasicIntQueue with I n c r e m e n t i n g with Doubling
queue . p u t ( 1 0 )
queue . g e t ( ) / / ? ? ?

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 24 / 29


Scala Hierarchy [8]

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 25 / 29


Access Modifiers [6] in Scala

public
protected
private
protected[<name>]
private[<name>]
Example,
package Assignment
...
protected [ Assignment ] v a l f i e l d 1 = 100
private [ t h i s ] val f i e l d 2 = 2;

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 26 / 29


Summary

What are still in your mind?

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 27 / 29


References I

[1] Classes and Objects, http://www.artima.com/pins1ed/


classes-and-objects.html, 19 06 2014.

[2] Traits, http://www.artima.com/pins1ed/traits.html,


19 06 2014.
[3] Case Class and Pattern Matching, http://www.artima.com/
pins1ed/case-classes-and-pattern-matching.html,
19 06 2014.
[4] Abstract Members, http:
//www.artima.com/pins1ed/abstract-members.html,
19 06 2014.
[5] Compositions and Inheritance, http://www.artima.com/
pins1ed/composition-and-inheritance.html, 19 06
2014.
[6] Packages and Imports, http://www.artima.com/pins1ed/
packages-and-imports.html, 19 06 2014.

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 28 / 29


References II

[7] Functional Objects, http://www.artima.com/pins1ed/


functional-objects.html, 19 06 2014.

[8] Scala Hierarchy, http:


//www.artima.com/pins1ed/scalas-hierarchy.html,
19 06 2014.

[9] Learning Scala part seven -Traits, http://joelabrahamsson.


com/learning-scala-part-seven-traits/, 19 06 2014.

Dr. Nguyen Hua Phung Object-Oriented Programming in Scala 29 / 29

You might also like