Super is a keyword in java which refers to the immediate super class object.
Use of super keyword in java:
super can be used to call immediate super class constructor (constructor chaining).
super can be used to call immediate super class method on a subclass object from a subclass method.
super can be used to access immediate super class instance variable.
1. super can be used to call immediate super class constructor (constructor chaining).
Example:
SuperExample1.java
/**
* This program is used to show the use of super
* keyword to invoke super class constructor using
* super explicitly.
* @author W3spoint
*/class Display {
Display(){System.out.println("Super class constructor called.");}}publicclass SuperExample1 extends Display {
SuperExample1(){//super keyword will call super class constructor.super();System.out.println("Current class constructor called.");}publicstaticvoid main(String args[]){
SuperExample1 obj =new SuperExample1();}}
/**
* This program is used to show the use of super
* keyword to invoke super class constructor using
* super explicitly.
* @author W3spoint
*/
class Display {
Display(){
System.out.println("Super class constructor called.");
}
}
public class SuperExample1 extends Display {
SuperExample1(){
//super keyword will call super class constructor.
super();
System.out.println("Current class constructor called.");
}
public static void main(String args[]){
SuperExample1 obj = new SuperExample1();
}
}
If super is not used explicitly compiler will automatically add super as the first statement.
Example:
SuperExample2.java
/**
* This program is used to show the use of super
* keyword to invoke super class constructor using
* super implicitly.
* @author W3spoint
*/class Display {
Display(){System.out.println("Super class constructor called.");}}publicclass SuperExample2 extends Display {
SuperExample2(){//compiler automatically add super here.System.out.println("Current class constructor called.");}publicstaticvoid main(String args[]){
SuperExample2 obj =new SuperExample2();}}
/**
* This program is used to show the use of super
* keyword to invoke super class constructor using
* super implicitly.
* @author W3spoint
*/
class Display {
Display(){
System.out.println("Super class constructor called.");
}
}
public class SuperExample2 extends Display {
SuperExample2(){
//compiler automatically add super here.
System.out.println("Current class constructor called.");
}
public static void main(String args[]){
SuperExample2 obj = new SuperExample2();
}
}
2. super can be used to call immediate super class method on a subclass object from a subclass method.
If super class and subclass have same methods and that method is called from subclass method than subclass method is called.
Example:
SuperExample4.java
/**
* This program is used to show that if super class and subclass
* have same methods and that method is called from subclass
* method than subclass method is called.
* @author W3spoint
*/class Display {publicvoid display(){System.out.println("display method of super class.");}}class Show extends Display {publicvoid display(){System.out.println("display method of sub class.");}publicvoid show(){System.out.println("show method of sub class.");//subclass display method is called.
display();}}publicclass SuperExample4 {publicstaticvoid main(String args[]){//create Show class object.
Show obj =new Show();//method call
obj.show();}}
/**
* This program is used to show that if super class and subclass
* have same methods and that method is called from subclass
* method than subclass method is called.
* @author W3spoint
*/
class Display {
public void display(){
System.out.println("display method of super class.");
}
}
class Show extends Display {
public void display(){
System.out.println("display method of sub class.");
}
public void show(){
System.out.println("show method of sub class.");
//subclass display method is called.
display();
}
}
public class SuperExample4 {
public static void main(String args[]){
//create Show class object.
Show obj = new Show();
//method call
obj.show();
}
}
Output:
show method of sub class.
display method of sub class.
show method of sub class.
display method of sub class.
/**
* This program is used to show the use super keyword
* to invoke the super class method from subclass method.
* @author W3spoint
*/class Display {publicvoid display(){System.out.println("display method of super class.");}}class Show extends Display {publicvoid display(){System.out.println("display method of sub class.");}publicvoid show(){System.out.println("show method of sub class.");//super class display method is called.super.display();}}publicclass SuperExample3 {publicstaticvoid main(String args[]){//create Show class object.
Show obj =new Show();//method call
obj.show();}}
/**
* This program is used to show the use super keyword
* to invoke the super class method from subclass method.
* @author W3spoint
*/
class Display {
public void display(){
System.out.println("display method of super class.");
}
}
class Show extends Display {
public void display(){
System.out.println("display method of sub class.");
}
public void show(){
System.out.println("show method of sub class.");
//super class display method is called.
super.display();
}
}
public class SuperExample3 {
public static void main(String args[]){
//create Show class object.
Show obj = new Show();
//method call
obj.show();
}
}
Output:
show method of sub class.
display method of superclass.
show method of sub class.
display method of super class.
If super class and subclass not have same methods and method of super class is called from subclass method than super class method is called. There is no need of super keyword.
Example:
SuperExample5.java
/**
* This program is used to show that if super class and subclass
* not have same methods and method of super class is called from
* subclass method than super class method is called.There is
* no need of super keyword.
* @author W3spoint
*/class Display {publicvoid display(){System.out.println("display method of super class.");}}class Show extends Display {publicvoid show(){System.out.println("show method of sub class.");//no need of super keyword here.
display();}}publicclass SuperExample5 {publicstaticvoid main(String args[]){//create Show class object.
Show obj =new Show();//method call
obj.show();}}
/**
* This program is used to show that if super class and subclass
* not have same methods and method of super class is called from
* subclass method than super class method is called.There is
* no need of super keyword.
* @author W3spoint
*/
class Display {
public void display(){
System.out.println("display method of super class.");
}
}
class Show extends Display {
public void show(){
System.out.println("show method of sub class.");
//no need of super keyword here.
display();
}
}
public class SuperExample5 {
public static void main(String args[]){
//create Show class object.
Show obj = new Show();
//method call
obj.show();
}
}
Output:
show method of sub class.
display method of superclass.
show method of sub class.
display method of super class.
3. super can be used to access immediate super class instance variable.
If super class and subclass have same instance variables and that variable is called from subclass than subclass instance variable will be referred.
Example:
SuperExample6.java
/**
* This program is used to show that if super class and subclass
* have same variable name and that variable is used in subclass
* method than subclass variable will be called.
* @author W3spoint
*/class Display {int num =100;}class Show extends Display {int num =200;publicvoid show(){//sub class instance variable will be referred.System.out.println("num = "+ num);}}publicclass SuperExample6 {publicstaticvoid main(String args[]){//create Show class object.
Show obj =new Show();//method call
obj.show();}}
/**
* This program is used to show that if super class and subclass
* have same variable name and that variable is used in subclass
* method than subclass variable will be called.
* @author W3spoint
*/
class Display {
int num = 100;
}
class Show extends Display {
int num = 200;
public void show(){
//sub class instance variable will be referred.
System.out.println("num = " + num);
}
}
public class SuperExample6 {
public static void main(String args[]){
//create Show class object.
Show obj = new Show();
//method call
obj.show();
}
}
/**
* This program is used to show the use super keyword to invoke
* the super class instance variable from subclass method.
* @author W3spoint
*/class Display {int num =100;}class Show extends Display {int num =200;publicvoid show(){//super class instance variable will be referred.System.out.println("num = "+super.num);}}publicclass SuperExample7 {publicstaticvoid main(String args[]){//create Show class object.
Show obj =new Show();//method call
obj.show();}}
/**
* This program is used to show the use super keyword to invoke
* the super class instance variable from subclass method.
* @author W3spoint
*/
class Display {
int num = 100;
}
class Show extends Display {
int num = 200;
public void show(){
//super class instance variable will be referred.
System.out.println("num = " + super.num);
}
}
public class SuperExample7 {
public static void main(String args[]){
//create Show class object.
Show obj = new Show();
//method call
obj.show();
}
}
If sub class and super class instance variables are not same than there is no need of super keyword.
Example:
SuperExample8.java
/**
* This program is used to show that if sub class and
* super class instance variables are not same than there
* is no need of super keyword.
* @author W3spoint
*/class Display {int num =100;}class Show extends Display {publicvoid show(){//super class instance variable will be referred.System.out.println("num = "+ num);}}publicclass SuperExample8 {publicstaticvoid main(String args[]){//create Show class object.
Show obj =new Show();//method call
obj.show();}}
/**
* This program is used to show that if sub class and
* super class instance variables are not same than there
* is no need of super keyword.
* @author W3spoint
*/
class Display {
int num = 100;
}
class Show extends Display {
public void show(){
//super class instance variable will be referred.
System.out.println("num = " + num);
}
}
public class SuperExample8 {
public static void main(String args[]){
//create Show class object.
Show obj = new Show();
//method call
obj.show();
}
}