SlideShare a Scribd company logo
1 of 9
Download to read offline
Scala:	
  Case	
  Classes	
  
Let's	
  model	
  the	
  following	
  
•  A	
  product	
  has	
  a	
  name,	
  category	
  and	
  an	
  
op;onal	
  descrip;on	
  
•  A	
  category	
  has	
  a	
  name	
  and	
  may	
  have	
  a	
  parent	
  
category	
  
•  All	
  the	
  above	
  a>ributes	
  are	
  read-­‐only	
  
public class Category {

private String name;
private Category parent;

public Category(String name, Category parent) {
this.name = name;
this.parent = parent;
}

public String getName() {
return name;
}

public Category getParent() {
return parent;
}
}
public class Product {

private String name;
private String description;
private Category category;

// TODO: What am I missing?
public Product(String name, String description, Category category) {
this.name = name;
this.description = description;
this.category = category;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}

public Category getCategory() {
return category;
}
}
And	
  in	
  Scala?	
  
class Category(
val name: String, 
val parent: Option[Category])

class Product(
val name: String, 
val description: String, 
val category: Category)
Or	
  even	
  be>er,	
  
case class Category(
name: String, 
parent: Option[Category])

case class Product(
name: String, 
description: String, 
category: Category)
What	
  you	
  get	
  for	
  free	
  
•  Companion	
  object	
  with	
  factory-­‐like	
  apply	
  
methods	
  
•  Class	
  parameters	
  become	
  fields	
  
•  Nicer	
  toString	
  
•  A>ribute	
  based	
  equality	
  
•  Pa>ern	
  matching	
  
•  Copy	
  method	
  to	
  'clone'	
  instances	
  
	
  
Be	
  mindful	
  of	
  
•  Bytecode	
  overhead	
  
•  Generated	
  code	
  might	
  clu>er	
  your	
  interface	
  
•  Case	
  class	
  inheritance	
  
Demonstrating Case Classes in Scala

More Related Content

Viewers also liked

Case class scala
Case class scalaCase class scala
Case class scala
Matt Hicks
 
Run-Away-Bear (1984)
Run-Away-Bear (1984)Run-Away-Bear (1984)
Run-Away-Bear (1984)
James Beers
 
James D. Beers CV (5-2-16)
James D. Beers CV (5-2-16)James D. Beers CV (5-2-16)
James D. Beers CV (5-2-16)
James Beers
 
Ferdigheter for en nav veileder
Ferdigheter for en nav  veilederFerdigheter for en nav  veileder
Ferdigheter for en nav veileder
unnihelene50
 

Viewers also liked (16)

Case class scala
Case class scalaCase class scala
Case class scala
 
Pattern Matching in Scala
Pattern Matching in ScalaPattern Matching in Scala
Pattern Matching in Scala
 
Intro to pattern matching in scala
Intro to pattern matching in scalaIntro to pattern matching in scala
Intro to pattern matching in scala
 
Run-Away-Bear (1984)
Run-Away-Bear (1984)Run-Away-Bear (1984)
Run-Away-Bear (1984)
 
Finishing brands atomization technology - vietnamese
Finishing brands   atomization technology - vietnameseFinishing brands   atomization technology - vietnamese
Finishing brands atomization technology - vietnamese
 
A Good Designed Website Relieves Your Business
A Good Designed Website Relieves Your BusinessA Good Designed Website Relieves Your Business
A Good Designed Website Relieves Your Business
 
James D. Beers CV (5-2-16)
James D. Beers CV (5-2-16)James D. Beers CV (5-2-16)
James D. Beers CV (5-2-16)
 
Ferdigheter for en nav veileder
Ferdigheter for en nav  veilederFerdigheter for en nav  veileder
Ferdigheter for en nav veileder
 
One stop english
One stop englishOne stop english
One stop english
 
Corporate intro presentation
Corporate intro presentationCorporate intro presentation
Corporate intro presentation
 
Introduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive PlatformIntroduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive Platform
 
Banner locations of Pragyan
Banner locations of PragyanBanner locations of Pragyan
Banner locations of Pragyan
 
Corner Jetted Bathtubs
Corner Jetted BathtubsCorner Jetted Bathtubs
Corner Jetted Bathtubs
 
6 benso, alban. - da micro-historia
6   benso, alban. - da micro-historia6   benso, alban. - da micro-historia
6 benso, alban. - da micro-historia
 
Elektriskie lādiņi
Elektriskie lādiņiElektriskie lādiņi
Elektriskie lādiņi
 
Week #7 looking for killer b's
Week #7  looking for killer b'sWeek #7  looking for killer b's
Week #7 looking for killer b's
 

Similar to Demonstrating Case Classes in Scala

Advanced php
Advanced phpAdvanced php
Advanced php
hamfu
 

Similar to Demonstrating Case Classes in Scala (20)

Scala: Collections API
Scala: Collections APIScala: Collections API
Scala: Collections API
 
Lecture Notes
Lecture NotesLecture Notes
Lecture Notes
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part III
 
Learn Java Part 10
Learn Java Part 10Learn Java Part 10
Learn Java Part 10
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
10 classes
10 classes10 classes
10 classes
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Java session4
Java session4Java session4
Java session4
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
14 Defining classes
14 Defining classes14 Defining classes
14 Defining classes
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 

More from BoldRadius Solutions

More from BoldRadius Solutions (17)

Partial Functions in Scala
Partial Functions in ScalaPartial Functions in Scala
Partial Functions in Scala
 
Towards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The BayTowards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The Bay
 
How You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in ProductionHow You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in Production
 
Introduction to the Actor Model
Introduction to the Actor ModelIntroduction to the Actor Model
Introduction to the Actor Model
 
Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?
 
String Interpolation in Scala | BoldRadius
String Interpolation in Scala | BoldRadiusString Interpolation in Scala | BoldRadius
String Interpolation in Scala | BoldRadius
 
Value Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadiusValue Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadius
 
Scala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadiusScala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadius
 
What Are For Expressions in Scala?
What Are For Expressions in Scala?What Are For Expressions in Scala?
What Are For Expressions in Scala?
 
Domain Driven Design Through Onion Architecture
Domain Driven Design Through Onion ArchitectureDomain Driven Design Through Onion Architecture
Domain Driven Design Through Onion Architecture
 
Pattern Matching in Scala
Pattern Matching in ScalaPattern Matching in Scala
Pattern Matching in Scala
 
What are Sealed Classes in Scala?
What are Sealed Classes in Scala?What are Sealed Classes in Scala?
What are Sealed Classes in Scala?
 
How To Use Higher Order Functions in Scala
How To Use Higher Order Functions in ScalaHow To Use Higher Order Functions in Scala
How To Use Higher Order Functions in Scala
 
Immutability in Scala
Immutability in ScalaImmutability in Scala
Immutability in Scala
 
Scala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching TypesafeScala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching Typesafe
 
Code Brevity in Scala
Code Brevity in ScalaCode Brevity in Scala
Code Brevity in Scala
 
Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the Effort
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 

Demonstrating Case Classes in Scala

  • 2. Let's  model  the  following   •  A  product  has  a  name,  category  and  an   op;onal  descrip;on   •  A  category  has  a  name  and  may  have  a  parent   category   •  All  the  above  a>ributes  are  read-­‐only  
  • 3. public class Category { private String name; private Category parent; public Category(String name, Category parent) { this.name = name; this.parent = parent; } public String getName() { return name; } public Category getParent() { return parent; } }
  • 4. public class Product { private String name; private String description; private Category category; // TODO: What am I missing? public Product(String name, String description, Category category) { this.name = name; this.description = description; this.category = category; } public String getName() { return name; } public String getDescription() { return description; } public Category getCategory() { return category; } }
  • 5. And  in  Scala?   class Category( val name: String, val parent: Option[Category]) class Product( val name: String, val description: String, val category: Category)
  • 6. Or  even  be>er,   case class Category( name: String, parent: Option[Category]) case class Product( name: String, description: String, category: Category)
  • 7. What  you  get  for  free   •  Companion  object  with  factory-­‐like  apply   methods   •  Class  parameters  become  fields   •  Nicer  toString   •  A>ribute  based  equality   •  Pa>ern  matching   •  Copy  method  to  'clone'  instances    
  • 8. Be  mindful  of   •  Bytecode  overhead   •  Generated  code  might  clu>er  your  interface   •  Case  class  inheritance