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.
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.
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.
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.
Dictates arguments that are sent up the object ancestor chain
super - sends all arguments passed to the function to parent
super() - no arguments
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