Instantly share code, notes, and snippets.

Embed
What would you like to do?
open class Animal { // Parent class
var name: String? = null // Nullable variable
var legs: Int = 0 // Non-nullable variable
lateinit var map: HashMap<Integer, String> // Variable inited later in the code
constructor(legs: Int) {
this.legs = legs
}
constructor(legs: Int, name: String) {
this.legs = legs
this.name = name
}
// open keyword allows the function to be overridden
open fun speak() : String? {
return null
}
}
class Dog : Animal { // Child class
constructor(legs: Int) : super(legs) {
// Optional code block
}
// Just a super call, without additional code block
constructor(legs: Int, name: String) : super(legs, name)
// Function over-ridding
override fun speak(): String? {
return "Bark! Bark!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment