Buaetin Nadir

Object-oriented Programming scala
Object-oriented Programming
is a programming paradigm, concept of
programming that focuses on using objects to
design and build applications.
Object . . ?

●

●

●

●

Object is everything in the real world that are
meaningful to your application.
An object is a software bundle of related state and
behavior.
An object is an instance of class.
Real-world objects share two characteristics: They
all have state and behavior. Example : Bicycles
have state (current gear, current pedal cadence,
current speed) and behavior (changing gear,
changing pedal cadence, applying brakes).
Object . . ?
Class ?

A class is a blueprint or prototype from
which objects are created.
Object and Class
Object
1

Object
2
Class

Object
3

Object
4
Instance Class
How to create an object in scala ?
How to create an object in scala ?
●

Creating object
Inheritance ?
●

●

●

is creating a new class with inherit the
characteristics existing class.
Inherited class called Superclass.
while class that inherits called subclass.
Subclass inherits all the methods and
variables Superclass, it is the same as
copying code from another class.
Inheritance ?
Vehicle

Car

Sedan

Bus

Bicycle
Inheritance ?
Inheritance ?
Inheritance ?
Classes and Objects and Traits
Classes
●

Syntax: class ClassName(parameters) { body }

●

The class definition is the (primary) constructor
Parameters and body are optional

●

Parameters, if any, are marked with:
–

var
●

–

val
●

–

A var parameter will cause a field, getter, and setter to be included
A val parameter will create a field and a getter, but no setter

Neither val nor var
●

Can be used within the body of the class, but not create a field or
any methods
Classes
scala>class Person(name: String, val age:Int, var city:String)
defined class Person
scala> val temon = new Person("Temon", 17, “Yogyakarta”)
temon: Person = Person(Temon)
scala> temon.name
<console>:10: error: value name is not a member of Person
temon.name
^
scala> temon.age
res3: Int = 17
scala> temon.age = 10
<console>:9: error: reassignment to val
temon.age = 10
^
scala> temon.city
res4: String = Yogyakarta
scala> temon.city = “Madiun”
temon.city: String = Madiun
●

●
Object
●

●

An object is defined similar to the way
that a class is defined, but it cannot take
parameters
Syntax: object ObjectName { body }
Companion objects
●

●

Scala’s equivalent of static is the
companion object
The companion object of a class
–

–

●

has the same name as the class
is defined in the same file as the class

The object and class can access each
other’s private fields and methods
Companion Objects
class Person(val name:String) {
private var age = 0
override def toString = name + "-" +age
}
object Person {
def apply(name:String, age:Int) = {
val person = new Person(name)
person.age = age
person
}
def apply(name:String) = new Person(name)
}
●

●
●

Call Person class ==>
println(Person("temon", 17)) // output : temon-17
println(Person("surya")) // output : surya-0
Abstract classes
Abstract class is actually a class. so it has all the properties of a
regular class (has a constructor).
Abstract class can create an empty field or method, which will
be implemented by its subclasses.
Abstract class will always be the superclass / highest hierarchy
of its subclasses.
As in Java, an abstract class is one that cannot be instantiated
In a concrete subclass, you do not need the override keyword
Syntax : abstract class ClassName(parameters) { body }
Case classes
●

Syntax: case class ClassName(parameters) { body }

●

All the parameters are implicitly val
–

A parameter can be explicitly declared as var

●

toString, equals, hashCode, and copy are generated

●

apply and unapply are also generated
–

–

apply lets you use when you create objects.
unapply lets you use the objects in pattern
matching.
Case clasess can be pattern matched
scala> case class Person(age: Int, name: String)
defined class Person
scala> val ibuBudi = Person(40, "Ibu Budi")
dave: Person = Person(40,Ibu Budi)
scala> ibuBudi match {
| case Person(a, n) if a > 30 => println(n + " is old!")
| case _ => println("Whatever")
|}
Ibu Budi is old!
scala> val budi = Person(17, "Budi")
quinn: Person = Person(17,Budi)
scala> budi match {
| case Person(a, n) if a > 30 => println(n + " is old!")
| case _ => println("Whatever")
|}
Whatever
Traits
●

Traits are like Java’s interfaces

●

Syntax: trait TraitName { body }

●

Unlike Java, traits may have concrete (defined) methods.

●

A class extends exactly one other class, but may with any
number of traits.

●

As in Java, trait is one that cannot be instantiated

●

Syntax:
–

–

class ClassName(parameters) extends OtherClass with
Trait1, …, TraitN { body }
class ClassName(parameters) extends Trait1 with Trait2, …,
TraitN { body }
Traits

Animal

Vehicle

Plane

Car

Bird

Monkey

Flying

Human

SuperMan

// trait

Programmer
Terima Kasih ...

Oop scala

  • 1.
  • 2.
    Object-oriented Programming is aprogramming paradigm, concept of programming that focuses on using objects to design and build applications.
  • 3.
    Object . .? ● ● ● ● Object is everything in the real world that are meaningful to your application. An object is a software bundle of related state and behavior. An object is an instance of class. Real-world objects share two characteristics: They all have state and behavior. Example : Bicycles have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).
  • 4.
  • 5.
    Class ? A classis a blueprint or prototype from which objects are created.
  • 6.
  • 7.
    How to createan object in scala ?
  • 8.
    How to createan object in scala ? ● Creating object
  • 9.
    Inheritance ? ● ● ● is creatinga new class with inherit the characteristics existing class. Inherited class called Superclass. while class that inherits called subclass. Subclass inherits all the methods and variables Superclass, it is the same as copying code from another class.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
    Classes ● Syntax: class ClassName(parameters){ body } ● The class definition is the (primary) constructor Parameters and body are optional ● Parameters, if any, are marked with: – var ● – val ● – A var parameter will cause a field, getter, and setter to be included A val parameter will create a field and a getter, but no setter Neither val nor var ● Can be used within the body of the class, but not create a field or any methods
  • 16.
    Classes scala>class Person(name: String,val age:Int, var city:String) defined class Person scala> val temon = new Person("Temon", 17, “Yogyakarta”) temon: Person = Person(Temon) scala> temon.name <console>:10: error: value name is not a member of Person temon.name ^ scala> temon.age res3: Int = 17 scala> temon.age = 10 <console>:9: error: reassignment to val temon.age = 10 ^ scala> temon.city res4: String = Yogyakarta scala> temon.city = “Madiun” temon.city: String = Madiun ● ●
  • 17.
    Object ● ● An object isdefined similar to the way that a class is defined, but it cannot take parameters Syntax: object ObjectName { body }
  • 18.
    Companion objects ● ● Scala’s equivalentof static is the companion object The companion object of a class – – ● has the same name as the class is defined in the same file as the class The object and class can access each other’s private fields and methods
  • 19.
    Companion Objects class Person(valname:String) { private var age = 0 override def toString = name + "-" +age } object Person { def apply(name:String, age:Int) = { val person = new Person(name) person.age = age person } def apply(name:String) = new Person(name) } ● ● ● Call Person class ==> println(Person("temon", 17)) // output : temon-17 println(Person("surya")) // output : surya-0
  • 20.
    Abstract classes Abstract classis actually a class. so it has all the properties of a regular class (has a constructor). Abstract class can create an empty field or method, which will be implemented by its subclasses. Abstract class will always be the superclass / highest hierarchy of its subclasses. As in Java, an abstract class is one that cannot be instantiated In a concrete subclass, you do not need the override keyword Syntax : abstract class ClassName(parameters) { body }
  • 21.
    Case classes ● Syntax: caseclass ClassName(parameters) { body } ● All the parameters are implicitly val – A parameter can be explicitly declared as var ● toString, equals, hashCode, and copy are generated ● apply and unapply are also generated – – apply lets you use when you create objects. unapply lets you use the objects in pattern matching.
  • 22.
    Case clasess canbe pattern matched scala> case class Person(age: Int, name: String) defined class Person scala> val ibuBudi = Person(40, "Ibu Budi") dave: Person = Person(40,Ibu Budi) scala> ibuBudi match { | case Person(a, n) if a > 30 => println(n + " is old!") | case _ => println("Whatever") |} Ibu Budi is old! scala> val budi = Person(17, "Budi") quinn: Person = Person(17,Budi) scala> budi match { | case Person(a, n) if a > 30 => println(n + " is old!") | case _ => println("Whatever") |} Whatever
  • 23.
    Traits ● Traits are likeJava’s interfaces ● Syntax: trait TraitName { body } ● Unlike Java, traits may have concrete (defined) methods. ● A class extends exactly one other class, but may with any number of traits. ● As in Java, trait is one that cannot be instantiated ● Syntax: – – class ClassName(parameters) extends OtherClass with Trait1, …, TraitN { body } class ClassName(parameters) extends Trait1 with Trait2, …, TraitN { body }
  • 24.
  • 25.