help me in this code kOTLIN !!!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

help me in this code kOTLIN !!!!

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". 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") } } class Image(width: Int, height: Int): Component(width, height) { fun draw() { println(width.toString()+"x"+height.toString()) } } fun main(args: Array<String>) { val b1 = Button(200, 50) b1.tap() b1.show() val img = Image(300, 500) img.draw() img.show() } help me in this code

24th Feb 2022, 9:31 PM
SANA SIPOU
SANA SIPOU - avatar
2 Answers
+ 3
This is correct answer 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() }
28th Nov 2022, 3:49 AM
Imteyaz Ahmad
Imteyaz Ahmad - avatar
+ 2
when you implement an abstract class you have to override its methods with the keyword override. class Button(width: Int, height: Int): Component(width, height) { private var name: String = "Button" //here override fun show() { println("Showing a Button") } } class Image(width: Int, height: Int): Component(width, height) { fun draw() { println(width.toString()+"x"+height.toString()) } //here override fun show() { println(" Showing an Image") } }
25th Feb 2022, 7:36 AM
Bahhaⵣ
Bahhaⵣ - avatar