I want to call a method in the super class from Clojure. I am extending a Java class using :gen-class
.
(ns subclass.core
(:gen-class
:extends Baseclass))
(defn greet []
"Hello from core") ; how to call super.greet()?
(defn -main [& args]
(greet))
Java Baseclass
public class Baseclass {
public String greet() {
return "Hello from Baseclass";
}
}
Edit: as the linked example I tried
(ns subclass.core
(:gen-class
:extends Baseclass
:exposes-methods {greet pgreet})
(:import Baseclass))
(defn greet []
(.pgreet (Baseclass.)))
(defn -main [& args])
But when I call (greet) I am getting the error
IllegalArgumentException No matching field found: pgreet for class Baseclass clojure.lang.Reflector.getInstanceField (Reflector.java:271)
Is this the right way to call the super class method?
Update: Got it. We create a different method which will intern call the base class method.
https://en.wikibooks.org/wiki/Clojure_Programming/Examples/API_Examples/Java_Interaction#genclass
NB: that's not what the linked answer says.
(.pgreet (BaseClass .))
instead of (.pgreet this)`. There's a huge difference. - Nathan Davisthis
arg? Isn't it the object of the classBaseclass
? If not,(defn greet [this] (.pgreet this))
how do I invokegreet
? What do I pass forthis
arg? - user235273this
would be an object of a subclass of the classBaseClass
. TheBaseClass
has nopgreet
method -- it's thegen-class
ed on that has apgreet
method. - Nathan Davis