Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.scala.abstractclassesvstraits.abstractclasses

abstract class GroceryProduct(sku: String) {
val price: Double // abstract field
val tax: Double = 0.23 // concrete field
def calculateTaxFee(): Double = price * tax // concrete method
}

abstract class Product {
val sku: String
val price: Double
}

abstract class TaxCalculator { self: Product => // self type
val tax: Double
def calculateTaxFee(): Double = price * tax
}

class Orange(val sku: String, val price: Double) extends GroceryProduct("40303")
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.scala.abstractclassesvstraits.abstractclasses;

public class JavaApple extends GroceryProduct {
public JavaApple(String sku) {
super(sku);
}
@Override
public double price() {
return 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.scala.abstractclassesvstraits.traits

trait Product {
val sku: String
val price: Double
}
trait TaxCalculator { self: Product => // self type
val tax: Double
def calculateTaxFee(): Double = price * tax
}

trait GroceryProduct extends TaxCalculator with Product

object GroceryProduct {
class Orange(val sku: String, val price: Double, val tax: Double)
extends TaxCalculator
with Product
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.scala.abstractclassesvstraits.traits;

class JavaApple implements GroceryProduct {
@Override
public String sku() {
return null;
}

@Override
public double price() {
return 0;
}

@Override
public double tax() {
return 0;
}

@Override
public double calculateTaxFee() {
return GroceryProduct.super.calculateTaxFee();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.scala.abstractclassesvstraits.abstractclasses

import org.scalatest.{Matchers, WordSpec}

class GroceryProductUnitTest extends WordSpec with Matchers {
val orange = new Orange("someSku", 100.0)

"GroceryProduct" should {
"calculate tax fee" in {
orange.calculateTaxFee() shouldBe 23.0
}
}
"Class" should {
"not extends from multiple abstract classes" in {
"""class Orange(val sku: String, val price: Double, val tax: Double)
| extends TaxCalculator
| with Product""".stripMargin shouldNot compile
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.scala.abstractclassesvstraits.traits

import org.scalatest.{Matchers, WordSpec}

class GroceryProductUnitTest extends WordSpec with Matchers {
val orange = new GroceryProduct.Orange("someSku", 100.0, 0.23)

"GroceryProduct" should {
"calculate tax fee" in {
orange.calculateTaxFee() shouldBe 23.0
}
}
"Trait" should {
"not have constructor" in {
"""trait Orange(sku: String) {}""" shouldNot compile
}
}
}