SlideShare a Scribd company logo
1 of 25
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 ...

More Related Content

What's hot

Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 

What's hot (20)

Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Ifi7184 lesson4
Ifi7184 lesson4Ifi7184 lesson4
Ifi7184 lesson4
 
Ifi7184 lesson5
Ifi7184 lesson5Ifi7184 lesson5
Ifi7184 lesson5
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Ifi7184 lesson6
Ifi7184 lesson6Ifi7184 lesson6
Ifi7184 lesson6
 
class c++
class c++class c++
class c++
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
types of classes in java
types of classes in javatypes of classes in java
types of classes in java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
 
Swift classes
Swift classesSwift classes
Swift classes
 

Viewers also liked

Viewers also liked (20)

Mengamankan SSH ID
Mengamankan SSH IDMengamankan SSH ID
Mengamankan SSH ID
 
Intercept Analyze Data
Intercept Analyze DataIntercept Analyze Data
Intercept Analyze Data
 
Debian server
Debian serverDebian server
Debian server
 
Evaluasi user interface
Evaluasi user interfaceEvaluasi user interface
Evaluasi user interface
 
Usability test
Usability testUsability test
Usability test
 
Hadoop
HadoopHadoop
Hadoop
 
Bagaimana menjadi system administrator yang baik
Bagaimana menjadi system administrator yang baikBagaimana menjadi system administrator yang baik
Bagaimana menjadi system administrator yang baik
 
Omni plan
Omni planOmni plan
Omni plan
 
Blackbox And Whitebox Testing
Blackbox And Whitebox TestingBlackbox And Whitebox Testing
Blackbox And Whitebox Testing
 
Dynamic dns
Dynamic dnsDynamic dns
Dynamic dns
 
CAPISTRANO
CAPISTRANOCAPISTRANO
CAPISTRANO
 
Artificial intelligence deep learning
Artificial intelligence deep learningArtificial intelligence deep learning
Artificial intelligence deep learning
 
casperjs presentation
 casperjs presentation casperjs presentation
casperjs presentation
 
Seo
SeoSeo
Seo
 
The most technical mistakes in tech startup
The most technical mistakes in tech startupThe most technical mistakes in tech startup
The most technical mistakes in tech startup
 
Material Design With Polymer
Material Design With PolymerMaterial Design With Polymer
Material Design With Polymer
 
Search engine optimization
Search engine optimizationSearch engine optimization
Search engine optimization
 
Content marketing
Content marketingContent marketing
Content marketing
 
Best Practices For Writing Super Readable Code
Best Practices For Writing Super Readable CodeBest Practices For Writing Super Readable Code
Best Practices For Writing Super Readable Code
 
File carving
File carvingFile carving
File carving
 

Similar to Oop scala

Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 

Similar to Oop scala (20)

java
javajava
java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptx
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
COMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptxCOMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptx
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
Oop
OopOop
Oop
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
C# program structure
C# program structureC# program structure
C# program structure
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
 
Lecture2.pdf
Lecture2.pdfLecture2.pdf
Lecture2.pdf
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Oop 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).
  • 5. Class ? A class is a blueprint or prototype from which objects are created.
  • 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.
  • 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 }