inheritance and traits
DESCRIPTION
TRANSCRIPT
![Page 1: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/1.jpg)
Introducing Inheritance And TraitsIn Scala
Introducing Inheritance And TraitsIn Scala
Piyush MishraSoftware Consultant
Knoldus Software LLP
Piyush MishraSoftware Consultant
Knoldus Software LLP
![Page 2: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/2.jpg)
Topics CoveredTopics Covered
Inheritance
Traits
Mix-In Composition of traits into classes
Ordered Traits
Traits as Stackable modification
Option Pattern
Inheritance
Traits
Mix-In Composition of traits into classes
Ordered Traits
Traits as Stackable modification
Option Pattern
![Page 3: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/3.jpg)
Inheritance Inheritance Inheritance is a way by which an object of a class acquire properties
and behavior of object of other class.So Inheritance is used for code reuse.
In Scala we use “extendsextends” keyword to inherit properties and behavior from a class.This is same as Java
class Animalclass Bird extends Animal
Omitting extends means extends AnyRef
Inheritance is a way by which an object of a class acquire properties and behavior of object of other class.So Inheritance is used for code reuse.
In Scala we use “extendsextends” keyword to inherit properties and behavior from a class.This is same as Java
class Animalclass Bird extends Animal
Omitting extends means extends AnyRef
![Page 4: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/4.jpg)
Calling superclass constructorCalling superclass constructor
Subclasses must immediately call their superclass constructor
scala> class Animal(val name: String)defined class Animalscala> class Bird(name: String) extends Animal(name)defined class Bird
Subclasses must immediately call their superclass constructor
scala> class Animal(val name: String)defined class Animalscala> class Bird(name: String) extends Animal(name)defined class Bird
![Page 5: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/5.jpg)
Use the keyword final to prevent a class from being subclassed
Use the keyword final to prevent a class from being subclassed
Scala> final class Animaldefined class Animal
Scala> class Bird extends Animal <console>:8: error: illegal inheritance from final class Animal
Scala> final class Animaldefined class Animal
Scala> class Bird extends Animal <console>:8: error: illegal inheritance from final class Animal
![Page 6: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/6.jpg)
Use the keyword sealed to allow sub-classing only within the
same source file
Use the keyword sealed to allow sub-classing only within the
same source filesealed class Animal
class Bird extends Animal
class Fish extends Animal
This means, that sealed classes can only be subclassed by youbut not by others, i.e. you know all subclasses
sealed class Animal
class Bird extends Animal
class Fish extends Animal
This means, that sealed classes can only be subclassed by youbut not by others, i.e. you know all subclasses
![Page 7: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/7.jpg)
Use the keyword override to override a superclass memberUse the keyword override to
override a superclass memberclass Animal {val name = "Animal"}class Bird extends Animal {override val name = "Bird"}
class Animal {val name = "Animal"}class Bird extends Animal {override val name = "Bird"}
![Page 8: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/8.jpg)
Abstract classesAbstract classes
Use the keyword abstract to define an abstract class
abstract class Animal {val name: Stringdef hello: String}
Use the keyword abstract to define an abstract class
abstract class Animal {val name: Stringdef hello: String}
![Page 9: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/9.jpg)
Implementing abstract membersImplementing abstract members
Initialize or implement an abstract field or method to make itConcrete
class Bird(override val name: String) extends Animal {override def hello = "Beep"
}
Initialize or implement an abstract field or method to make itConcrete
class Bird(override val name: String) extends Animal {override def hello = "Beep"
}
![Page 10: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/10.jpg)
TraitsTraits
Traits are like IInterfaces nterfaces but they are richer than Java Interfaces
They are fundamental unit of code reuse in Scala
They encapsulates method and field definitions, which can be reused by mixing them in classes
Unlike class inheritance a class can mix any number of traits
Unlike Interfaces they can have concrete methods
Traits are like IInterfaces nterfaces but they are richer than Java Interfaces
They are fundamental unit of code reuse in Scala
They encapsulates method and field definitions, which can be reused by mixing them in classes
Unlike class inheritance a class can mix any number of traits
Unlike Interfaces they can have concrete methods
![Page 11: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/11.jpg)
Unlike Java interfaces traits can explicitly inherit from a class
Unlike Java interfaces traits can explicitly inherit from a class
class Atrait B extends Aclass Atrait B extends A
![Page 12: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/12.jpg)
Mix-In CompotitionMix-In CompotitionOne major use of traits is to automatically add methods to
class in terms of methods the class already has. That is, trait can enrich a thin interface,making it into a rich interface.
trait Swimmer {def swim() {println("I swim!")}}Use the keyword with to mix a trait into a class that alreadyextends another class
classclass Fish(val name: String) extends Animal with Swimmer
So method swim can mix into class Fish ,class Fish does not need to implement it.
One major use of traits is to automatically add methods to class in terms of methods the class already has. That is, trait
can enrich a thin interface,making it into a rich interface.
trait Swimmer {def swim() {println("I swim!")}}Use the keyword with to mix a trait into a class that alreadyextends another class
classclass Fish(val name: String) extends Animal with Swimmer
So method swim can mix into class Fish ,class Fish does not need to implement it.
![Page 13: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/13.jpg)
Mixing-in multiple traitsMixing-in multiple traits
Use the keyword with repeatedly to mix-in multiple traits
If multiple traits define the same members, the outermost(rightmost) one “wins”
Use the keyword with repeatedly to mix-in multiple traits
If multiple traits define the same members, the outermost(rightmost) one “wins”
Trait ATrait BTrait CClass D extends A with B with C
![Page 14: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/14.jpg)
Ordered Trait Ordered Trait
When-ever you compare two objects that are ordered, it is convenient if you use a single method call to ask about the precise comparison you want. if you want “is less than,” you would like to call <if you want “is less than or equal,” you would like to call <=
A rich interface would provide you with all ofthe usual comparison operators, thus allowing you to
directly write thingslike “x <= y”.
When-ever you compare two objects that are ordered, it is convenient if you use a single method call to ask about the precise comparison you want. if you want “is less than,” you would like to call <if you want “is less than or equal,” you would like to call <=
A rich interface would provide you with all ofthe usual comparison operators, thus allowing you to
directly write thingslike “x <= y”.
![Page 15: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/15.jpg)
Ordered Trait Ordered Trait
We have a class Number
class Number(a:Int) {val number =adef < (that: Number) =this.number < that.numberdef > (that: Number) = this.number > that.numberdef <= (that: Number) = (this < that) || (this == that)def >= (that: Number) = (this > that) || (this == that)}
We have a class Number
class Number(a:Int) {val number =adef < (that: Number) =this.number < that.numberdef > (that: Number) = this.number > that.numberdef <= (that: Number) = (this < that) || (this == that)def >= (that: Number) = (this > that) || (this == that)}
![Page 16: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/16.jpg)
Ordered Trait Ordered Trait
We have a class Number which extends ordered trait
So compare method provide us all comparison operators
We have a class Number which extends ordered trait
So compare method provide us all comparison operators
class Number(a:Int) extends Ordered[Number] { val number=a def compare(that:Number)={this.number-that.number}}
![Page 17: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/17.jpg)
Traits as stackable modificationsTraits as stackable modifications
Traits let you modify the methods of a class, and they doso in a way that allows you to stack those modifications with each other.
Given a class that implements such a queue, you could define traits toperform modifications such as these
Doubling: double all integers that are put in the queue
Incrementing: increment all integers that are put in the queue
Filtering: filter out negative integers from a queue
Traits let you modify the methods of a class, and they doso in a way that allows you to stack those modifications with each other.
Given a class that implements such a queue, you could define traits toperform modifications such as these
Doubling: double all integers that are put in the queue
Incrementing: increment all integers that are put in the queue
Filtering: filter out negative integers from a queue
![Page 18: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/18.jpg)
Traits as stackable modificationsTraits as stackable modifications
abstract class IntQueue {def get(): Intdef put(x: Int)}
class BasicIntQueue extends IntQueue {private val buf = new ArrayBuffer[Int]def get() = buf.remove(0)def put(x: Int) { buf += x }}
abstract class IntQueue {def get(): Intdef put(x: Int)}
class BasicIntQueue extends IntQueue {private val buf = new ArrayBuffer[Int]def get() = buf.remove(0)def put(x: Int) { buf += x }}
![Page 19: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/19.jpg)
Traits as stackable modificationsTraits as stackable modifications
val queue = new BasicIntQueue
queue.put(10)
queue.put(20)
queue.get() it will return 10
Queue.get() it will return 20
val queue = new BasicIntQueue
queue.put(10)
queue.put(20)
queue.get() it will return 10
Queue.get() it will return 20
![Page 20: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/20.jpg)
Traits as stackable modificationsTraits as stackable modifications
take a look at using traits to modify this behavior
trait Doubling extends IntQueue {abstract override def put(x: Int) { super.put(2 * x) }}
class MyQueue extends BasicIntQueue with Doublingval queue = new MyQueue
queue.put(10)queue.get() it will return 20
take a look at using traits to modify this behavior
trait Doubling extends IntQueue {abstract override def put(x: Int) { super.put(2 * x) }}
class MyQueue extends BasicIntQueue with Doublingval queue = new MyQueue
queue.put(10)queue.get() it will return 20
![Page 21: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/21.jpg)
Traits as stackable modificationsTraits as stackable modifications
Stackable modification traits Incrementing and Filtering.
trait Incrementing extends IntQueue {abstract override def put(x: Int) { super.put(x + 1) }}
trait Filtering extends IntQueue {abstract override def put(x: Int) {if (x >= 0) super.put(x)}}
Stackable modification traits Incrementing and Filtering.
trait Incrementing extends IntQueue {abstract override def put(x: Int) { super.put(x + 1) }}
trait Filtering extends IntQueue {abstract override def put(x: Int) {if (x >= 0) super.put(x)}}
![Page 22: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/22.jpg)
Traits as stackable modificationsTraits as stackable modifications
take a look at using traits to modify this behavior
val queue = (new MyQueue extends BasicIntQueue with Doublingwith Incrementing with Filtering)
queue.put(-1); queue.put(0); queue.put(1)queue.get() Int = 2
take a look at using traits to modify this behavior
val queue = (new MyQueue extends BasicIntQueue with Doublingwith Incrementing with Filtering)
queue.put(-1); queue.put(0); queue.put(1)queue.get() Int = 2 Filtering
Increamenting
Doubling
![Page 23: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/23.jpg)
Option TypeOption Type
Scala has a standard type named Option for optional values. Such a value can be of two forms. It can be of the form Some(x) where x is the actual value. Or it can be the None object, which represents a missing value
Scala has a standard type named Option for optional values. Such a value can be of two forms. It can be of the form Some(x) where x is the actual value. Or it can be the None object, which represents a missing value
![Page 24: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/24.jpg)
Option PatternOption Patternobject OptionPatternApp extends App {
val result = divide(2, 0).getOrElse(0) println(result)
def divide(x: Double, y: Double): Option[Double] = { try { Option(errorProneMethod(x, y)) } catch { case ex => None } }
def errorProneMethod(x: Double, y: Double): Double = { if (y == 0) throw new Exception else {x / y} }
}
object OptionPatternApp extends App {
val result = divide(2, 0).getOrElse(0) println(result)
def divide(x: Double, y: Double): Option[Double] = { try { Option(errorProneMethod(x, y)) } catch { case ex => None } }
def errorProneMethod(x: Double, y: Double): Double = { if (y == 0) throw new Exception else {x / y} }
}
![Page 25: Inheritance And Traits](https://reader034.vdocuments.us/reader034/viewer/2022042518/54631b7fb4af9f491c8b4914/html5/thumbnails/25.jpg)
ThanksThanks