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
puts "Hello Base"
class Derive < Base
puts "Hello Derived Base"
puts "Hello Derived"
# Creating instance of Base class
base = Base.new
# Creating instance of Derive class
derive = Derive.new
Hello Derived Base
Recommend the tutorial