Skip to main content
5 of 5
added 198 characters in body
user avatar
user avatar

How to call super class when extending a Java class using genclass in Clojure?

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.

user235273