geekelo / dsa_practice

This repo is maintained just for the purpose of daily data structures and algorithm practice and proficiency
1 stars 0 forks source link

What is the difference between calling super and calling super()? #1

Open geekelo opened 8 months ago

geekelo commented 8 months ago

In Ruby, super and super() are related but serve different purposes. Both are used in the context of inheritance and method overriding.

  1. super: This is used within a method of a subclass to invoke the same-named method from its superclass. It passes the arguments that were passed to the current method in the subclass.

    class Parent
      def example(arg)
        puts "Parent class, arg: #{arg}"
      end
    end
    
    class Child < Parent
      def example(arg)
        puts "Child class"
        super  # invokes the 'example' method in the Parent class with the same 'arg'
      end
    end
    
    Child.new.example("Hello")

    In this example, super is used to call the example method of the parent class with the argument passed to the method in the child class.

  2. super(): This is used to invoke the same-named method from the superclass but without passing any arguments explicitly.

    class Parent
      def example(arg)
        puts "Parent class, arg: #{arg}"
      end
    end
    
    class Child < Parent
      def example(arg)
        puts "Child class"
        super()  # invokes the 'example' method in the Parent class without passing 'arg'
      end
    end
    
    Child.new.example("Hello")

    Here, super() is used to call the example method of the parent class without passing the argument explicitly.

In summary, super without parentheses passes along the arguments implicitly, while super() explicitly calls the superclass method without passing any arguments. The choice between them depends on the specific requirements of the overridden method in the subclass.

geekelo commented 8 months ago

When you use super without parentheses in Ruby, it implicitly passes along the arguments that were passed to the current method. This means that the arguments received by the method in the subclass are automatically forwarded to the corresponding method in the superclass.

Let's illustrate this with an example:

class Parent
  def example(arg)
    puts "Parent class, arg: #{arg}"
  end
end

class Child < Parent
  def example(arg)
    puts "Child class"
    super  # 'arg' is implicitly passed along to the 'example' method in the Parent class
  end
end

Child.new.example("Hello")

In this example, when the example method is called on an instance of the Child class with the argument "Hello", the super keyword is used without parentheses. This means that the "Hello" argument is automatically passed along to the example method in the Parent class. The output will be:

Child class
Parent class, arg: Hello

So, by using super without parentheses, you allow Ruby to handle the passing of arguments from the subclass method to the superclass method implicitly.