Let’s look at a simple example, to understand how super
keyword works:
So, using super
we can call the same method as defined in the super-class. This helps us to re-use methods that exists in super-class, and modify them as per our needs in the subclass.
Difference between Super and Super()
When we use super
, Ruby invokes the method with same name from parent class,
along with the arguments that were passed to that method.
But, when we call with super()
, it doesn’t pass any arguments to the parent. You can use super()
when you just want to call the method inherited from Parent without passing any arguments.
And, you can use super(arg1, arg2, …)
to pass only some specified arguments to the inherited method.