Practice Time: Using Abstract and Interface
Practice Time
Abstract and Interface
Let's go back to your spices. Make Spice
an abstract class, and then create some subclasses that are actual spices.
- It's easiest (organizationally) if you make a new package,
Spices
, with a file, Spice, that has amain()
function. - Copy/paste your
Spice
class code into that new file. - Make
Spice
abstract. - Create a subclass,
Curry
.Curry
can have varying levels of spiciness, so we don't want to use the default value, but rather pass in the spiciness value. - Spices are processed in different ways before they can be used. Add an abstract method
prepareSpice
toSpice
, and implement it inCurry
. - Curry is ground into a powder, so let's call a method
grind()
. However, grinding is something that's not unique to curry, or even to spices, and it's always done in a grinder. So we can create an Interface,Grinder
, that implements thegrind()
method. Do that now. - Then add the
Grinder
interface to theCurry
class.
Delegation
Using the provided code from the lesson for guidance, add a yellow color to Curry
.
fun main (args: Array<String>) { delegate() } fun delegate() { val pleco = Plecostomus() println("Fish has has color ${pleco.color}") pleco.eat() } interface FishAction { fun eat() } interface FishColor { val color: String } object GoldColor : FishColor { override val color = "gold" } class PrintingFishAction(val food: String) : FishAction { override fun eat() { println(food) } } class Plecostomus (fishColor: FishColor = GoldColor): FishAction by PrintingFishAction("eat a lot of algae"), FishColor by fishColor
- Create an interface,
SpiceColor
, that has acolor
property. You can use a String for the color. - Create a singleton subclass,
YellowSpiceColor
, using theobject
keyword, because all instances ofCurry
and other spices can use the sameYellowSpiceColor
instance. - Add a
color
property toCurry
of typeSpiceColor
, and set the default value toYellowSpiceColor
. - Add
SpiceColor
as an interface, and let it be by color. - Create an instance of
Curry
, and print its color. However, color is actually a property common to all spices, so you can move it to the parent class. - Change your code so that the
SpiceColor
interface is added to theSpice
class and inherited byCurry
.
import Spices.Curry
// File: Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/19/2023
// Email: nolvertou@gmail.com
// Description: Using Abstract and Interface
fun main(args: Array<String>) {
delegate()
}
fun delegate(){
val curry = Curry("Tikka Masala", "hot" )
// TODO 2.5: Create an instance of Curry, and print its color.
println("The curry: ${curry.name} is of color: ${curry.color}")
curry.prepareSpice()
curry.grind()
}
package Spices
// TODO 1.4: Create a subclass, Curry. Curry can have varying levels of spiciness, so we don't want to use the default
// value, but rather pass in the spiciness value.
// TODO 2.3: Add a color property to Curry of type SpiceColor, and set the default value to YellowSpiceColor.
class Curry(name: String, spiciness: String, spiceColor: SpiceColor = YellowSpiceColor) :
Spice(name, spiciness, spiceColor), Grinder
{
override fun grind() {
println("You are grinding: $name")
}
override fun prepareSpice() {
println("You are preparing: $name")
}
}
// TODO 1.1: It's easiest (organizationally) if you make a new package, Spices, with a file, Spice, that has a main() function.
package Spices
// TODO 1.3: Make Spice abstract.
abstract class Spice(val name: String, val spiciness: String = "mild", spiceColor: SpiceColor) :
// TODO 2.4: Add SpiceColor as an interface, and let it be by spicecolor.
SpiceColor by spiceColor {
// TODO 1.5: Spices are processed in different ways before they can be used. Add an abstract method prepareSpice
// to Spice, and implement it in Curry.
abstract fun prepareSpice()
}
package Spices
// TODO 1.6: Curry is ground into a powder, so let's call a method grind(). However, grinding is something that's
// not unique to curry, or even to spices, and it's always done in a grinder. So we can create an Interface,
// Grinder, that implements the grind() method
interface Grinder {
fun grind()
}
package Spices
// TODO 2.1: Create an interface, SpiceColor, that has a color property. You can use a String for the color.
interface SpiceColor {
val color: String
}
package Spices
// TODO 2.2: Create a singleton subclass, YellowSpiceColor, using the object keyword, because all instances of Curry
// and other spices can use the same YellowSpiceColor instance
object YellowSpiceColor: SpiceColor {
override val color = "Yellow"
}
Comments
Post a Comment