Created
June 21, 2023 21:58
-
-
Save MamboBryan/1ef885ce9ab4c411f07179960a47ca73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bizyback.play.circletest | |
/** | |
* The implementation of the prepare function is abstracted from the callers | |
*/ | |
interface Food { | |
fun prepare(time: Int): String = "Preparing ${getIngredients()} as a ${this::class.simpleName} in $time minutes" | |
fun getIngredients(): String | |
} | |
data class Meal(val name: String = "biryani") : Food { | |
override fun getIngredients() = name | |
} | |
data class Salad(val ingredients: List<String> = listOf("tomato", "onions")) : Food { | |
override fun getIngredients() = ingredients.joinToString() | |
} | |
fun main() { | |
val salad: Salad = Salad() | |
val meal: Meal = Meal() | |
cook(salad) | |
cook(meal) | |
cook(salad as Food) | |
} | |
/** | |
* THE cook function is polymorphic and has different forms | |
*/ | |
fun cook(meal: Meal) { | |
println(meal.prepare(10)) | |
} | |
fun cook(salad: Salad) { | |
println(salad.prepare(15)) | |
} | |
fun cook(food: Food) { | |
println(food.prepare(2)) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment