Interview question and answer on Super keyword in Java

In this article, we will cover some of the interview questions with their justification on Super keyword in Java

These are most frequently asked interview question from Super keyword in Java

Read Super keyword in Java concepts in detail

 

Q) Explain the usage of super keyword ?

Usage of super keyword in Java:

  • Instance variable: super keyword is used to refer instance variables of immediate parent class
  • Super constructor: super() constructor call; is used to invoke constructor of immediate parent class
  • Overridden Method: <methodName>is used to invoke instance method of immediate parent class (when it is overridden in the sub-class)
  • Note:super keyword cannot be used to refer in static context

Q) Why always immediate parent class constructor is invoked ?

Rules for Constructor chaining:

  • Whenever an Object is created, then its respective class constructor is invoked
  • But before the execution of its own class constructor, its immediate parent class constructor is invoked
  • This is repeated till root of the hierarchy is reached i.e.; until Object class

Reason:

  • In every constructor, compiler by default inserts call to super class constructor using super(); statement
  • Therefore, every-time Object is created, its parent class constructor is invoked
  • Programmer can also write call to super(); constructor statement explicitly, but impacts is very same much to the default one
  • If this super() constructor call is present, then it must be 1st statement of the constructor
  • Otherwise, compile-time error will be thrown

Q) Does inserting explicit super(); constructor call inside child class constructor results in error ?

  • There won’t be any error by inserting explicit super() constructor call
  • Rather, inserting explicit super(); constructor call inside child class constructor results in invocation of parent class constructor
  • Let us insert super(); constructor explicitly in the child class and see result
  • Example, as shown in the below screen-capture

Q) How to invoke parent class’s parameterized constructor from child class ?

  • As programmer can write explicit call to super class constructor using super keyword from child class constructor
  • Likewise, using parameterized super(…) constructor call is also possible
  • This is required, whenever programmer required to invoke parameterized constructor of the immediate parent class
  • Let us see one example with its results (as shown in the below screen-capture)

Q) How to invoke parent class’s instance variable from child class, when variable names of both parent & child class are same ?

  • To invoke parent class variable from child class, use super keyword (when names of the variable in both parent & child class are same)
  • Syntax: super.<variable-name>
  • Example, as shown in the below screen-capture

Q) Whether it is possible to refer static variable of parent class from child class using super keyword ?

  • Although, it is very possible to access parent class’s static variable using super keyword
  • But its usage is discouraged with compiler warning with message “The static field <parent-class-name>.<static-variable-name> should be accessed in a static way
  • As it can be accessed using class-name
  • Syntax: <class-name>.<static-variable-name>
  • Let us see one example where we are accessing static variable of parent class using super keyword

Q) How to invoke parent class method from child class, if method is overridden in the child class ?

  • Suppose method is overridden in the sub-class from parent-class
  • Then to access overridden method of parent-class from child-class, use super keyword
  • Syntax: super.<overridden-method-name>
  • In the below example, display() is overridden from parent class
  • So to access the parent class display() method from child class display() method, we have used super keyword, as shown in the below screen-capture

 

In the next article, we will see couple of question on this keyword

 

References:

https://docs.oracle.com/javase/tutorial/java/IandI/super.html
http://www.benchresources.net/super-keyword-in-java/

 

Happy Coding !!
Happy Learning !!

Interview question and answer on this keyword in Java
Interview question and answer on Exception Handling in Java