Object orientation and        traits
Annotations – not marker interfaces      @serializable class Person      @SerialVersionUID(1) class Person      @cloneable...
objectobject is a “singleton” class  Call may look like static method calls in Java  Can inherit from other classes and tr...
Companion objectCan read the companion class private fieldsHas to be in the same source file class Person(private val age:...
Magical applyclass Person private(val age: Int)object Person {  def apply(age: Int) = new Person(age)}var personInstance =...
Not built in, clever use of apply val myList = List(1, 2, 3) val mySet = Set(1, 2, 3) val myMap = Map(1 -> "one", 2 -> "tw...
ConstructorsAlways one primary constructorParameters are automatically instance variablesClass “body” is the primary const...
Auxiliary constructorsclass MyClass(myString: String, myInt: Int) {    def this(myString: String) = this(myString, 0)    d...
Inheritanceclass MyClass(myString: String)class MySubClass(myString: String, myInt: Int)      extends MyClass(myString) { ...
InheritanceSingle class inheritanceMultiple trait mixins
Syntaxclass Person extends AnyRef with java.io.Serializable                            with java.rmi.Remoteclass Person ex...
Traits“Multiple inheritance done right”Implement methodsInitialized fieldsAbstract methodsAbstract fieldsAbstract typesDoe...
scala.Ordered traittrait Ordered[A] {  def compare(that: A): Int    def   <    (that:   A):   Boolean   =   (this   compar...
The Ordered traitclass Person(private val age: Int) extends Ordered[Person]{  def compare(other: Person) = this.age - othe...
“Dynamic mixins”class Person(age: Int) {  override def toString = "my age is " + age}trait MyTrait {  override def toStrin...
is-a vs has-aInheritance as usualEasier to implement has-aInheritance vs composition
Tasks (30 min)Open oo-traits projectTests under scalaexamples   companionobject   inheritance   traitsAdd @Test to one and...
Upcoming SlideShare
Loading in …5
×

1.6 oo traits

760 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
760
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

1.6 oo traits

  1. 1. Object orientation and traits
  2. 2. Annotations – not marker interfaces @serializable class Person @SerialVersionUID(1) class Person @cloneable class Person @remote class Service
  3. 3. objectobject is a “singleton” class Call may look like static method calls in Java Can inherit from other classes and traits Can be passed as a reference object MyObject { def foo = "bar" } var baz = MyObject.foo val personObject = MyObject baz = personObject.foo
  4. 4. Companion objectCan read the companion class private fieldsHas to be in the same source file class Person(private val age: Int) object Person { def getPersonAge(p: Person) = p.age } val personInstance = new Person(30) val age = Person.getPersonAge(personInstance)
  5. 5. Magical applyclass Person private(val age: Int)object Person { def apply(age: Int) = new Person(age)}var personInstance = Person.apply(30) personInstance = Person(30)
  6. 6. Not built in, clever use of apply val myList = List(1, 2, 3) val mySet = Set(1, 2, 3) val myMap = Map(1 -> "one", 2 -> "two")
  7. 7. ConstructorsAlways one primary constructorParameters are automatically instance variablesClass “body” is the primary constructors contentAuxiliary constructors MUST call or chain to primary constructor class MyClass(myString: String, myInt: Int) val myOtherInt = 10 println("In main body") }
  8. 8. Auxiliary constructorsclass MyClass(myString: String, myInt: Int) { def this(myString: String) = this(myString, 0) def this() = { this("foo") println("In default constructor") }}
  9. 9. Inheritanceclass MyClass(myString: String)class MySubClass(myString: String, myInt: Int) extends MyClass(myString) { println("MyString: " + myString + ", MyInt: " + myInt + "")}
  10. 10. InheritanceSingle class inheritanceMultiple trait mixins
  11. 11. Syntaxclass Person extends AnyRef with java.io.Serializable with java.rmi.Remoteclass Person extends java.io.Serializable with java.rmi.Remote
  12. 12. Traits“Multiple inheritance done right”Implement methodsInitialized fieldsAbstract methodsAbstract fieldsAbstract typesDoes not have constructorsCall to super -> strict semantics
  13. 13. scala.Ordered traittrait Ordered[A] { def compare(that: A): Int def < (that: A): Boolean = (this compare that) < 0 def > (that: A): Boolean = (this compare that) > 0 def <= (that: A): Boolean = (this compare that) <= 0 def >= (that: A): Boolean = (this compare that) >= 0}
  14. 14. The Ordered traitclass Person(private val age: Int) extends Ordered[Person]{ def compare(other: Person) = this.age - other.age}val person1 = new Person(21)val person2 = new Person(31)person1 < person2 // trueperson1 <= person2 // trueperson1 >= person2 // false
  15. 15. “Dynamic mixins”class Person(age: Int) { override def toString = "my age is " + age}trait MyTrait { override def toString = "I am sure " + super.toString}val person = new Person(30) with MyTraitprintln(person)=> I am sure my age is 30
  16. 16. is-a vs has-aInheritance as usualEasier to implement has-aInheritance vs composition
  17. 17. Tasks (30 min)Open oo-traits projectTests under scalaexamples companionobject inheritance traitsAdd @Test to one and one methodFollow instructions in codeMake tests pass

×