« Return to Thread: Java subclasses in Ruby

Re: Java subclasses in Ruby

by Nick Sieger-2 :: Rate this Message:

Reply to Author | View in Thread

On 3/7/07, backspaces <owen@...> wrote:

> First, I build a trivial java class with a setup method and a util method
> which simply prints a string.  In addition, I add a method that simply calls
> this instance's setup method:
> public class JavaClass {
>   public void setup() {
>     util("JavaClass: setup called.");
>   }
>   public void util(String s) {
>     System.out.println(s);
>   }
>   public void callSetup() {
>     this.setup();
>   }
> }
>
> I then build a java subclass overriding setup(), and include a main which
> lets me test it.  The test creates an instance of JavaClass and an instance
> of the subclass.  It then calls setup directly, and indirectly via
> callSetup:
> public class JavaSubClass extends JavaClass {
>     public void setup() {
>       util("JavaSubClass: setup called.");
>     }
>     public static void main(String[] args) {
>       JavaClass jc = new JavaClass();
>       jc.setup();
>       jc.callSetup();
>       System.out.println("");
>       JavaSubClass jsc = new JavaSubClass();
>       jsc.setup();
>       jsc.callSetup();
>     }
> }
>
> When I run it, I get what you'd expect:
> owen|~/src/jruby[1110]: java JavaSubClass
> JavaClass: setup called.
> JavaClass: setup called.
>
> JavaSubClass: setup called.
> JavaSubClass: setup called.
>
> Next for the JRuby test:
> require 'java'
> include_class "JavaClass"
>
> javaClass = JavaClass.new
> javaClass.setup()
> javaClass.callSetup()
>
> class RubyClass < JavaClass
>   def setup()
>     util('RubyClass: setup called')
>   end
> end
>
> puts
> rubyClass = RubyClass.new
> rubyClass.setup()
> rubyClass.callSetup()
>
> .. which works just fine:
> owen|~/src/jruby[1111]: jruby testjava.rb
> JavaClass: setup called.
> JavaClass: setup called.
>
> RubyClass: setup called
> RubyClass: setup called

I'm not surprised that this works, calling the Ruby subclass from the
Ruby side.  What happens if you pass that object back to Java and try
to call the setup method from there?

/Nick

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

 « Return to Thread: Java subclasses in Ruby