0

What happens when we use super() in a constructor to pass elements to a base class

13th Oct 2020, 4:20 AM
Grace Maria Binu
Grace Maria Binu - avatar
3 Réponses
+ 1
Grace Maria Binu The super keyword in java invokes the constructor of the parent class. The JVM automatically uses the super keyword to invokes the constructor of the parent class. As you know every class inherits the Object class, so whenever we create an object of any class, the JVM automatically invokes the constructor of Object class. The super keyword is used to perform constructor chaining. We can call a default constructor of parent class by use of super() keyword (without arguments), to call a parameterized constructor of parent class we can use super(arguments) keyword(with arguments). For more details: https://javagoal.com/java-super-keyword/
13th Oct 2020, 7:08 AM
Raina Dhankhar
Raina Dhankhar - avatar
+ 4
super() is basically a call from your child class constructor to your parent class constructor. You can pass arguments and initialize parent class instance variables through super() since your parent class constructor is executed first. Always remember that the super() should always be used on the first line inside your constructor.
13th Oct 2020, 5:13 AM
Avinesh
Avinesh - avatar
+ 2
A super() call invokes the constructor of the extending class or the default constructor if the class doesn't have one. Rather than passing elements to an extended class, you should declare them in the extended class and then initialize them in the subclass using super.intVar = 4;
13th Oct 2020, 4:38 AM
none
none - avatar