What is the use of super keyword in Java?

  1. Use of super keyword with variables
    1. 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.
  2. Use of super keyword with methods
    1. 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.
  3. Use of super keyword with Constructors
    1. super keyword can also be used to access the parent class constructor.
    2. Other Important points:
      1. Call to super() must be first statement in Derived(Student) Class constructor.
      2. 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.

Author: Mahesh

Technical Lead with 10 plus years of experience in developing web applications using Java/J2EE and web technologies. Strong in design and integration problem solving skills. Ability to learn, unlearn and relearn with strong written and verbal communications.