Program to illustrate the use of super keyword

in this java tutorial we are learn about super keyword in java.
the super keyword is a reference variable which is used to refer immediately parent class object.
whenever you create the instance of sub class, an instance of parent class is created implicitly which is reference by supper reference variable. 

use of supper keyword.
  1. supper can be use to refer immediate parent class instance variable.
  2. supper can be use to invoke immediate parent class method.
  3. supper() can be used to invoke immediate parent class constructor.                                   

Code :

 

class person

 

{

int id;

 

String name;

 

public person(int n,String str)

 

{

System.out.println("This constructor is of class person");

 

id=n;

 

name=str;

 

}

public void display()

 

{

 

System.out.println("Your no.:"+id); System.out.println("Your name:"+name);

 

}

 

}

 

class emp02 extends person{

 

float salary;

 

public emp02(int n,String str,float s)

{

 

super(n,str);

 

System.out.println("This is the constructor of class emp."); salary=s;

}

 

public void display()

 

{

 

super.display();

System.out.println("Your salary:"+salary);

}

}

 

 

class sup

 

{

 

public static void main(String[] args) {

 

emp02 obj=new emp02(9,"kiran",14000.00f); obj.display();

 

}

 

}

output:

super keyword


Post a Comment

0 Comments