Your SlideShare is downloading. ×
Oop scala
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Oop scala

234
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
234
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Buaetin Nadir Object-oriented Programming scala
  • 2. Object-oriented Programming is a programming 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. Object . . ?
  • 5. Class ? A class is a blueprint or prototype from which objects are created.
  • 6. Object and Class Object 1 Object 2 Class Object 3 Object 4 Instance Class
  • 7. How to create an object in scala ?
  • 8. How to create an object in scala ? ● Creating object
  • 9. 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.
  • 10. Inheritance ? Vehicle Car Sedan Bus Bicycle
  • 11. Inheritance ?
  • 12. Inheritance ?
  • 13. Inheritance ?
  • 14. Classes and Objects and Traits
  • 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 is defined similar to the way that a class is defined, but it cannot take parameters Syntax: object ObjectName { body }
  • 18. 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
  • 19. 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
  • 20. 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 }
  • 21. 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.
  • 22. 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
  • 23. 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 }
  • 24. Traits Animal Vehicle Plane Car Bird Monkey Flying Human SuperMan // trait Programmer
  • 25. Terima Kasih ...