## 2019年4月7日日曜日

### Kotlin - subclasses and superclasses - prefix(open、override)、functions、properties

Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 5(subclasses and superclasses - Using Your Inheritance)、Code Magnets(141)の解答を求めてみる。

コード

```open class Animal {
open val image = ""
open val food = ""
open val habitat = ""
var hunger = 10

open fun makeNoise() {
println("The Animal is making a noise")
}

open fun eat() {
println("The Animal is eating")
}

open fun roam() {
println("The Animal is roaming")
}
}

open class Canine : Animal() {
override fun roam() {
println("The Canine is roaming")
}
}

class Wolf : Canine() {
override val image: String = "wolf.jpg"
override val food = "meat"
override val habitat = "forests"
override fun makeNoise() {
println("Hooooowl!")
}

override fun eat() {
println("The Wolf is eating \$food")
}
}

fun main() {
val a = Animal()
val c = Canine()
val w = Wolf()

a.makeNoise()
a.eat()
a.roam()
println()
c.makeNoise()
c.eat()
c.roam()
println()
w.makeNoise()
w.eat()
w.roam()
}```

```The Animal is making a noise
The Animal is eating
The Animal is roaming

The Animal is making a noise
The Animal is eating
The Canine is roaming

Hooooowl!
The Wolf is eating meat
The Canine is roaming

Process finished with exit code 0
```