Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Tutorials Jruby Method Overriding in JRuby

Related Tutorials


Share on Google+Share on Google+

Method Overriding in JRuby

In this part of JRuby tutorial you will know to implement overriding of methods between the classes.

Method Overriding in JRuby


In this part of JRuby tutorial you will know to implement overriding of methods between the classes. In earlier examples of JRuby you have studied a lot about to create class, inheriting class and calling methods of classes. Now in this example we will introduce you with the case of overriding of methods in JRuby program.

In this example we have created a class "Base" and it consists of one method "addBase" and after that we have created another class Derive which consists of one new method addDerive  and one overridden method addBase which have some other functionality rather than Base class by inheriting the Base class.

After creating instances of Base and Derive class when we will call the overridden then it will show you the case of overriding that Base class method is overridden.Here is the example code of OverrideJRuby.rb as follows:




# Example program of Inheritance in JRuby

class Base
  def addBase
   puts "Hello Base"

class Derive < Base
  def addBase 
   puts "Hello Derived Base"

   def addDerive
  puts "Hello Derived"
# Creating instance of Base class
  base =
  puts base.addBase

# Creating instance of Derive class
   derive =
   puts derive.addBase
   puts derive.addDerive


C:\JRuby>jruby OverrideJRuby.rb
Hello Base
Hello Derived Base
Hello Derived

Download Source Code



If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 29, 2008

Related Tutorials

Discuss: Method Overriding in JRuby   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image