what is super keyword in java?

when you have a super class and subclass, super class variables and methods will be inherited to subclass i.e memory will be allocated for the super class variables whenever you create a subclass object and super class object; then the method will be accessed with subclass object.
In this situation we use the super() keyword to inherit super class member and method.


Some point to be noted regarding Super Keyword:
  • Super class constructor can't be inherited with the subclass.
  • Since super class constructor can't be inherited to subclass so we need a mechanism to invoke a super class constructor without creating object for the subclass; we can achieve using super keyword.    Using super we can invoke immediately super class constructor.
  • We can have arguments super also as:
  • Super()------to invoke super class default constructor
  • Super(a)----to invoke super class one argument constructor


Some points more:
  •  When no constructor available in the class then default constructor will be invoked by JVM automatically
  • If class contain any constructor then JVM will not insert any constructor
  • When no super statement inside the subclass constructor, then default super statement "Super()" will be inserted by the JVM automatically
  • If any super statement is available in the subclass constructor then JVM will not insert super statement.
  • Only one super statement will be allow inside the constructor 
  • Super statement is used to invoke immediate super class constructor.
  • "Super" statement and "this" statement must be the first statement inside the constructor either Super(); or this().









0 comments :

Post a Comment