0
Kotlin abstract practice lesson 32.2
Can anyone help me with this question, please and thank you. You decide to make the Component class abstract, since you are not going to create objects of that type. You also add a show() function to it that the derived classes need to override. For a Button, the show() function should output: "Showing a Button", while for an Image it should output: "Showing an Image". Implement the required show() function to generate the expected output. // my code https://code.sololearn.com/cgGM09i5ZZ4l/?ref=app
3 Answers
+ 3
abstract class Component(width: Int, height: Int) {
protected var width = width
protected var height = height
abstract fun show()
}
class Button(width: Int, height: Int): Component(width, height)
{
private var name: String = "Button"
fun tap()
{
println(name + " was tapped")
}
override fun show(){
println("Showing a Button")
}
}
class Image(width: Int, height: Int): Component(width, height)
{
fun draw() {
println(width.toString()+"x"+height.toString())
}
override fun show(){
println("Showing an Image")
}
}
fun main(args: Array<String>) {
val b1 = Button(200, 50)
b1.tap()
b1.show()
val img = Image(300, 500)
img.draw()
img.show()
what is wrong with my code
+ 2
how may i help you ?
code is already working
+ 2
Capital letters
println("Showing a Button") /* Button */
println("Showing an Image") /* Image */