super vs this :
| super keyword always refers to the parent elements like parent method, parent constructor, parent instance variables. | this keyword always refers to the current class elements like methods, constructors, instance variables. |
| Calling parent class method by using super. Example: super.parentClassMethodName(); |
Calling current class method Example: this.currentClassMethodName(); |
| Calling parent class constructor. Example: super();, super(100); |
Calling current class constructor. Example: this();, this(100); |
| super keyword always refers to the parent elements like parent method, parent constructor, parent instance variables. | this keyword always refers to the current class elements like methods, constructors, instance variables. |
| Using parent class variable in child class. super.a=100; |
Using current class variable. this.a=100; |
Use of super and this keyword:
Example
class A{
int temp=10;
void show()
{
System.out.println("A Sho w");
}
}
class B extends A{
int temp=20;
void show(){
super.show(); //Use of super to call parent class method
System.out.println("B Show");
this.temp=super.temp+this.temp;
}
}
class Test{
public static void main(String args[]){
B obj=new B();
obj.show();
}
}
Another use of super and this keyword:
class A{
A(){
System.out.println("A Class Default Constructor Call");
}
A(int a){
System.out.println("A class Parameterized
Constructor call ");
}
}
class B extends A{
B(){
System.out.println("B Class Default Constructor Call ");
}
B(int t){
super(10); //Use of super to call parent class
parameterized constructor and it must be the
first line.
System.out.println("B Class Parameterized
Constructor call");
}
}
Constructor Chaining
If we consider a scenario in which a base class is extended by a child class. Whenever an object of the child class is created , the constructor of the parent class is invoked first.This is called Constructor chaining.
- Constructor chaining example :
class Example { int value1; int value2; Example() { value1 = 1; value2 = 2; System.out.println("First Parent Constructor"); } Example(int a) { value1 = a; System.out.println("Second Parent Constructor"); } public void display() { System.out.println("Value1 "+value1); System.out.println("Value2 "+value2); } public static void main(String args[]) { ExampleChild d1 = new ExampleChild(); d1.display(); } } class ExampleChild extends Example{ int value3; int value4; ExampleChild() { //super(1); value3 = 3; value4 = 4; System.out.println("Child Constructor"); } public void display() { System.out.println("Value1 "+value1); System.out.println("Value2 "+value2); System.out.println("Value1 "+value3); System.out.println("Value2 "+value4); } }
Another Example:
package com.techknow public class ConsChaining { public ConsChaining(){ System.out.println("Default constructor..."); } public ConsChaining(int i){ this(); System.out.println("Single parameterized constructor..."); } public ConsChaining(int i,int j){ this(j); System.out.println("Double parameterized constructor..."); } public static void main(String a[]){ ConsChaining ch = new ConsChaining(10,20); } }



