this and super interview-type questions



This content originally appeared on DEV Community and was authored by KIRAN RAJ

  1. What is this keyword in Java?
  • This is a reference variable in Java.

  • It refers to the current object (the object whose method or constructor is being executed).

  1. Where is this used?
  • You can use this in many places:

  • To refer to current class instance variables when they are shadowed by method/constructor parameters.

  • To call current class methods.

  • To call one constructor from another constructor (constructor chaining).

  • To return the current class object.

  • To pass the current object as an argument in method calls or constructors.

  1. When do we use this?
  • When local variables (method/constructor parameters) have the same name as instance variables → to avoid confusion.

  • When you want to chain constructors inside the same class.

  • When you want to pass the current object to another method/class.

  1. How do we use this?
  • How → Using this.variable, this(), this.method(), return this, method(this).
  1. Why is this used?
  • To resolve ambiguity between instance variables and local variables.

  • To improve code readability and avoid errors.

  • To allow constructor chaining.

  • To refer to the current object flexibly.

  1. What is super?
  • Super is a reference keyword in Java that refers to the immediate parent class (superclass) of the current object.

  • It helps to access parent class members (variables, methods, constructors) that are hidden or overridden in the child class.

  1. Where is super used?
  • Inside a child class that extends a parent class.

You can use it:

  • With variables → to access parent class fields.

  • With methods → to call parent class methods.

  • With constructors → to call parent class constructors.

  1. When is super used?
  • When the child class has a variable or method with the same name as the parent class (to avoid confusion).

  • When you need to invoke the parent’s overridden method from the child class.

  1. How is super used?
  • If child and parent have the same variable name, use super.variableName to get the parent’s value.

  • If child overrides a parent’s method, super.methodName() lets you call the parent’s version.

  1. Why is super?
  • To avoid ambiguity when the child and parent have the same variable/method.

  • To reuse parent class logic instead of rewriting it.


This content originally appeared on DEV Community and was authored by KIRAN RAJ