Super in java is a keyword which is a reference variable. It is used to refer to the immediate superclass object or instance variable. The word super came into usage because of the concept of inheritance. The keyword super is placed inside a subclass to call a method from a superclass.
Let's say we have a super class as Person and Employee as a sub class as shown below:
package com.javabykiran.Super_This;
public class Person {
int age = 45;
}
package com.javabykiran.Super_This;
public class Employee extends Person{
int age = 29;
void printStudent() {
int age = 25;
//Here are two ways we have to call global age variable
//of Person class
// 1
Person person = new Person();
System.out.println(person.age); //45
// 2
System.out.println(super.age); // 45
//Local variable
System.out.println(age); //25
//Global variable of same class System.out.println(this.age); //29
}
}
Now, if we see Employee class's printStudent() method, there are two ways to call age variable of Person class.
First way is by creating object of Person class. But this is not an affordable option as just for sake of one variable age here we are loading complete class Person, which wastes memory unnecessarily.
Second way is to just call age by using super keyword which will not waste memory and we can achieve our objectives as well
Other two options are to print local variable age or print global variable of same class.
Note : We use the super keyword to bypass class variables and method’s local variable with same name while calling and we need not to create objects. It's all about memory management.
Remember the points given below:
Syntax:
super.a; //for Variable
super.m1(); //for Method
super(); // for Constructor
Example:
A a=new A(); //1st way
a.m2();
super.m2(); //2nd way
1st way : In this case we bring all members of capital A in to RAM, out of that we are just calling m2(), so unnecessary memory will get allocated for another members.
2nd way : The other option super.m2()
There is no super.super in java
In any of our class's constructors if we don't write any call to constructor of parent class by using super like super(), then JVM puts super call to constructor in the first line of every constructor wherever you did not add your own call to constructor.
Example 1
package com.constructor;
public class A {
public A(){
super(10); //Error -Super class has constructor
//without parameter, in this case it’s a Object class
}
}
Example 2
public class A {
public A(){ // Default Constructor put by JVM at Run
//Time..
super(); //invisible present here
}
}
Super call to constructor must be at first line of all constructors.
We strictly cannot call super () in methods in any line; this rule applies only to super() not to super.a or super.m1().
package com.constructor;
public class A {
public A(){
System.out.println("Java by kiran");
super(); // Error - super () must be at first line
// of constructor only
}
void m1(){
super(); // Error in method super()not allowed
}
A (int x){
super(); // Correct- Allowed in constructor
super(10); // Error as super() is call at second line
}
}
The compile time error is shown below :
package com.javabykiran.Super_This;
public class A extends A{ //class can not extends itself
//super(); // go on calling the same -- recursion
}
Note: In this example, recursion between super call to constructor will be there because we are extending same class itself. Consider invisible super() inside constructor.
Observe the example below to see how super keyword navigates the flow of our program.
package com.constructor;
public class A {
A(){
//super(); //By default super is here
System.out.println("A");
}
A(int x) {
//super(); //By default super is here
System.out.println(x);
}
public void m6() {
System.out.println("I am in A m6()");
}
} //end of class A
package com.constructor;
public class B extends A{
B() {
//super(); //JVM put super() by default at runtime
}
public void m6(){
System.out.println("I am in B m6()");
}
} //end of class B
package com.constructor;
public class C extends A {
C(){
//super(); // JVM put super() by default at runtime
System.out.println("C");
}
} // end of class C
package com.constructor;
public class Client {
public static void main(String[] args) {
C c= new C();
c.m6();
}
}
Output:
A
C
I am in A m6()
This keyword in java is used inside a method or constructor of a class. It is used to refer to a member of a current object within the instance of a method or constructor.
Syntax:
this.a; // variable case
this.m1(); // method case
this(10); // To call parameterize constructor of current class object
In case of variable, same class’s global variable will get called
Call to constructor by using this ‘this()’ must be a first line of constructor only. This means that we cannot add ‘this()’ anywhere other than the first line of constructor
inside method ‘this()’ is not allowed, that is call to constructor not allowed by using this.
JVM never puts automatically this() keyword like super()
If we wrote call to constructor explicitly by using ‘this()’ the ‘super()’ call to constructor will not be put by JVM
Recursion will be there as we call the same class constructor using this() in same constructor, which is not allowed in java
Example 1
package com.This;
public class A {
int a=10;
}
package com.This;
public class B extends A {
int a=20;
}
package com.This;
public class C extends A{
int a=30;
}
package com.This;
public class D extends C {
int a=40;
void m1(){
int a=50;
System.out.println("This is javabykiran class");
System.out.println(a); //prints 50
System.out.println(this.a); //class variable value 40
System.out.println(super.a); //JVM print 30 Immediate //superclass
B b =new B();
System.out.println(b.a); //no other way as super.super is not allowed
A a1=new A();
System.out.println(a1.a); //only way to run A class variable
}
public static void main(String[] args) {
D b=new D();
b.m1();
}
} //end D
Output:
This is java by kiran class
50
40
30
20
10
Note : To understand this example in more detail
Example 2
package com.This;
class X {
public X() {
this(20); //recursion
}
public X(int i){
this();//recursion invocation error
}
}
Super | This |
---|---|
|
|
|
|
|
|