Step down at my blog with your ideas,comments,suggestions on Ruby on Rails, PHP or Web2.0 Technology.

Friday, June 30, 2006

Overloading Constructors

I tried the below Pgm. Just help me for my title Qs.
# Constructors can be overloaded or not?

class MotorCycle
#def initialize()
# puts 'no argument'
# end

def initialize(just_one)
puts 'one argument'
end

puts "Just one constructor"
#puts "With Two constructor"
#obj1=MotorCycle.new();
obj2=MotorCycle.new('one');

end

output 1: Putting the # as in above code

C:\ruby\bp>ruby cs.rb

Just one constructor
one argument

output 2: Remove those # in above code
C:\ruby\bp>ruby cs.rb
With Two constructor
cs.rb:12:in `initialize': wrong number of arguments (0 for 1) (ArgumentError)
from cs.rb:12:in `new'
from cs.rb:12

0 Comments:

Post a Comment

<< Home