- Use of super keyword with variables
- This scenario occurs when a derived class and base class has same data members. In that case there is a possibility of ambiguity for the JVM.
- Use of super keyword with methods
- This is used when we want to call parent class method. So, whenever a parent and child class have same named methods then to resolve ambiguity, we use super keyword.
- Use of super keyword with Constructors
- super keyword can also be used to access the parent class constructor.
- Other Important points:
- Call to super() must be first statement in Derived(Student) Class constructor.
- If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that a whole chain of constructors called, all the way back to the constructor of Object. This, in fact, is the case. It is called constructor chaining.