Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between calling super and calling super()

Tags:

What is the difference between calling super and calling super()? Which is the best one if the arguments passed to the child method don’t match what the parent is expecting.

like image 521
Bibek Sharma Avatar asked Aug 04 '15 18:08

Bibek Sharma


People also ask

What is difference between super and super()?

The super keyword in Java is a reference variable that is used to refer parent class objects. The super() in Java is a reference variable that is used to refer parent class constructors. super can be used to call parent class' variables and methods. super() can be used to call parent class' constructors only.

What is super in Ruby on Rails?

Ruby uses the super keyword to call the superclass implementation of the current method. Within the body of a method, calls to super acts just like a call to that original method. The search for a method body starts in the superclass of the object that was found to contain the original method.


2 Answers

When you call super with no arguments, Ruby sends a message to the parent of the current object, asking it to invoke a method with the same name as where you called super from, along with the arguments that were passed to that method.

On the other hand, when called with super(), it sends no arguments to the parent.

If the arguments you have don't match what the parent is expecting, then I would say you would want to use super(), or explicitly list parameters in the functional call to match a valid parent constructor.

like image 94
Michael Vessia Avatar answered Oct 13 '22 21:10

Michael Vessia


Dictates arguments that are sent up the object ancestor chain

super - sends all arguments passed to the function to parent
super() - no arguments
 
like image 31
Austio Avatar answered Oct 13 '22 22:10

Austio