Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor overriding

Tags:

super

ruby

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) 
like image 547
ceth Avatar asked Apr 03 '10 06:04

ceth


People also ask

What is constructor overloading and overriding?

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.

What is constructor overriding in C++?

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.

Can constructor be overloaded vs overridden?

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.

What is constructor overloading with example?

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.


1 Answers

super in this case (without parentheses) is a special form. It calls the superclass method with the original params.

Instead try calling

super() 
like image 166
gtd Avatar answered Oct 05 '22 11:10

gtd