to study inheritance at Cotlin I'm asking you this question because there is a confusing part of the child instance property approach in the parent instance
open class Animal(val size:Int)
class Spider(val poison:Int): Animal(50)
fun main() {
val a1:Animal = Spider(5)
println("posion : ${a1.poison}")
}
where a1 is inaccessible to poison. Because a1 is an animal type
Creating a child type in a parent type in an inheritance relationship?Parent instances only for parent type properties
I learned that it's accessible
By the way,
open class Animal(val size:Int)
class Spider(val poison:Int): Animal(50)
fun main() {
val s1 = Spider(5)
val a1:Animal
a1 = s1
println("posion : ${a1.poison}")
}
Why is this accessible?
a1 was created as an animal type.
I don't understand.
kotlin
fun main() {
val s1 = Spider(5)
// // val a1:Animal
val a1 = s1
println("posion : ${a1.poison}")
}
It's like this, so it's not an animal type. It's accessible because it's a spider type.
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
581 PHP ssh2_scp_send fails to send files as intended
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.