oop - The cast can never be succeed -


i new in kotlin , study concept of oop

i trying cast code, i'm facing error:

open class operations1(){ open  fun sum(n1:int , n2:int):int{     return n1+ n2 } fun sub(n1:int , n2:int):int{     return n1- n2 } } class multioperations1():operations(){ override  fun sum(n1:int , n2:int):int{     return n1+ n2 +5 } fun mul(n1:int , n2:int):int{     return n1* n2 } fun div(n1:int , n2:int):int{     return n1/ n2 } } fun main(args:array<string>){  var oper = operations() var inlit  = multioperations1() operations1 println("enter first number") var n1:int = readline()!!.toint() println("enter second number") var n2:int = readline()!!.toint()  var sum = inlit.sum(n1 , n2) var sub = inlit.sub(n1 , n2) println("sum: " + sum) println("sub: " + sub) } 

screen shot of code

this screen shot of code

error:

this error program through

you seem have both operations , operations1 class. multioperations1 class inherits operations instead of operations1, won't able cast operations1 (unless operations subclass of operations1).

i assume wanted inherit operations1 instead, this:

class multioperations1(): operations1() {     ... } 

a note on conventions: class names in kotlin follow java conventions, , use upper camel case, should name class multioperations1 instead.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -