1

Here two classes are given class A & B. class A has a extension function as a sub() and derived class B also has a same name function sub().If the extension function is member function of class A then why it can not be access by super keyword ????

open class A(var s : Int){
    var a : Int = 5
    var b : Int = 4
    var c : Int = 0
    constructor() : this(7) {
        //println("class A constructor has been called !!!")
        println("primary constructor value is $s")
    }
   fun add(h:Int) {
        c = a.plus(b)
        println("h is for class A $h")
    }
    fun dis(){
        println("sum of a & b is $c")

    }
}
fun A.sub(){

    println("sub of class A is ${a.minus(b)}")

}
class B: A{

    constructor(){
        println("primary constructor for class B")

    }
    fun sub(){
        super.sub();
        println("sub of class B is ${a.minus(b)}")

    } 

}
fun main(args:Array<String>){
    var b = B()
    b.add(2)
    b.dis()
    b.sub()
}
  • No, the extension function is not a member function of class A. – yole Jul 06 '18 at 17:07
  • This could help you: [Extension functions](https://kotlinlang.org/docs/reference/extensions.html#extensions-are-resolved-statically) – pixix4 Jul 06 '18 at 17:33
  • See also this [link](https://stackoverflow.com/questions/51170152/extension-function-in-a-kotlin-using-super/51170685#51170685) –  Jul 06 '18 at 19:24
  • @yole ..i know maybe its true or not but i needed reason.. as per via super keyword its not accessible then its prove that its not member function.. – chirag contractor Jul 07 '18 at 01:46

2 Answers2

1

Extension functions are not members of the class. They are essentially static methods that conveniently appear as a member when typing them in Kotlin.

You are not able to access any private members of receiver object:

class MyClass(private val b : Int) { }

fun MyClass.extFun() = b // Cannot access 'b': it is private in 'MyClass'

When calling it from Java the syntax is following:

MyClassKt.extFun(myClass);  // if extension is in file MyClass.kt
Pawel
  • 15,548
  • 3
  • 36
  • 36
1

Extension function is a member function , but i don't know wht we can't call it using super keyword, but we have it's alternative way to call it like super.. So modified your code as shown below..

fun A.sub(){
    println("sub of class A is ${a.minus(b)}")
}
class B: A{
    constructor(){
        println("primary constructor for class B")
    }
    fun sub(){ 
         (this as A).sub()
         println("sub of class B is ${a.minus(b)}")
    } 
}
hardik gosai
  • 328
  • 3
  • 17
  • Thanks bro ... its works !! And maybe its specially added for extension function..!! – chirag contractor Jul 08 '18 at 08:44
  • Extension functions are not members of the class that's why you can't override them or use super and that's why you used typecasting. –  Jul 08 '18 at 08:46
  • Also look at this [extension function in a kotlin using super ](https://stackoverflow.com/questions/51170152/extension-function-in-a-kotlin-using-super/51170685#51170685) –  Jul 08 '18 at 08:48
  • @mTak .. as i got answer .. is it one kind of static method !? – chirag contractor Jul 08 '18 at 17:27
  • No it's not static or kind of static. From the documentation: "Kotlin, ..., provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator." –  Jul 08 '18 at 19:10
  • Ohk may be you are right but i think it will not work when we want to implement inherent concept.. – chirag contractor Jul 09 '18 at 04:20