this keyword:
- What is this keyword in Java?
this refers to the current object of the class.
this refers to the instance variable of the current object.
2.When do we use this keyword?
__To refer to the current instance variable
- If local variable and instance variable have the same name, this helps avoid confusion.
__To call the current class method
- Inside a class, we can use this to call another method of the same class.
__To call another constructor in the same class (Constructor Chaining)
- this() is used to call another constructor of the same class.
3.How is this keyword used in Java?
this.variable → access current object variable
this.method() → call a method of same class
this() → call another constructor in same class
4.Why do we use this keyword?
To avoid confusion between local variables and instance variables.
To reuse constructors (constructor chaining).
To make code cleaner and more readable.
To refer to current object in methods, especially in OOP concepts
like passing objects, builder patterns, etc.
5.where we use this Keyword java?
in constructors --- to call another constructor
in instance method --- to access Variables/method of the same object.
when passing the Current Object --- as a Parameter
Super keyword:
1.what is Super keyword in Java?
The Super keyword is refer to the Immediate Parent class object.
allowing a Subclass to access its Parent's members.
The Super keyword refers to Superclass (parent) object.
2.when we use Super Keyword in java?
Super Keyword is used to call Superclass method and to access the Superclass Constructor.
when a child class overrides a method but you still need the parent
class you use super keyword.when Parent and child class Variables have the same name we use Super.
``
3.How it will be used?
super.variable ---- access parent class variable.
super.method() ---- call parent class method.
super() ---- call parent class constructor (must be first statement).
4.where we use super keyword in java?
__To access parent class variables
used Inside a Child Class Construtor.
must be the first statement in the Constructor.
__To access Parent Class Variables
- IF both Parrent and child have a Variable with the Same name we use super to access the Parent Version.
__ Tο access Parent class methods
→if a method is overridden in child Class,we can still call the parent's method using super.
5.why we use super Keyword?
__To avoid Variable shadowing.
- If the child class has a Variable with the Same name as the Parent the child's variable hides the Parent's.
→using Super.VariableName, we can still access the parent's version.
__To call the Parent's method when overridden
when the child overrides a method, Sometimes we still want to use the parent's method.
super.methodName() lets us call the parent's implementation.
Top comments (0)