Kotlin abstract practice lesson 32.2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

2nd Oct 2022, 2:37 PM
Brent Tyson
Brent Tyson - avatar
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
25th Nov 2022, 9:27 AM
mesuli mkhize
mesuli mkhize - avatar
+ 2
how may i help you ? code is already working
2nd Oct 2022, 2:54 PM
Davinder Kumar
Davinder Kumar - avatar
+ 2
Capital letters println("Showing a Button") /* Button */ println("Showing an Image") /* Image */
2nd Oct 2022, 3:46 PM
SoloProg
SoloProg - avatar