Super keyword is parent class. There should be inheritance otherwise super keyword will not work. In simple words, super keyword helps to access parent class in child class.

Super keyword does not execute in static content. Means this cannot be used in main method or in static method. But it will work with static variable.

For Example:

class A{

int i=20; }

class B extends A{

public static void main(){

B b= new B();

b.test();

}

public void test(){

System.out.println(super.i);

} }

Output: 20

We can call constructor. There is condition that this should be first statement while call constructor in another constructor. We cannot call it into method. We can call parent class constructor in child class constructor.

For Example:

class A{

A(){

System.out.println(“A”);

} }

class B extends A{

B(){

super();

}

public static void main(){

new B();

}

}

Output: A

If we have not written super keyword then JVM will print it for calling parent class constructor in child class

for example:

class A{

A(){

System.out.println(“A”);

} }

class B extends A{

B(){

super();

}

public static void main(){

new B();

}

}

Output: A

Note: If we did not write super keyword then JVM will write it automatically and call parent class constructor in child class constructor.

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?