I have a class:
class One def initialize; end end
I need to create a new class with my own constructor like this:
class Two < One def initialize(some) puts some super end end Two.new("thing")
but when I launch the code, I got an error:
thing test.rb:10:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)
The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task. Consider the following Java program, in which we have used different constructors in the class.
Constructor Overloading in C++ In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments. This concept is known as Constructor Overloading and is quite similar to function overloading.
Neither. Constructors are different from methods. You overload a constructor by writing multiple constructors in the same class, not in inherited classes. And constructors aren't subject to overriding.
Constructor overloading is a concept of having more than one constructor with different parameters list, in such a way so that each constructor performs a different task. For e.g. Vector class has 4 types of constructors.
super
in this case (without parentheses) is a special form. It calls the superclass method with the original params.
Instead try calling
super()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With