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.
- supper can be use to refer immediate parent class instance variable.
- supper can be use to invoke immediate parent class method.
- 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:
0 Comments