This and Super Keyword in Java
This and Super Keyword in Java
KEYWORD IN JAVA
PRESENTATION
BY
Mohammed
Shajahan
INDEX
Explanation of keyword
THIS
Uses of THIS
Program using THIS
Explanation of keyword
SUPER
Uses of SUPER
Program using SUPER
classStudent10{
intid;
Stringname;
student(intid,Stringname){
id=id;
name=name;
}
voiddisplay()
{System.out.println(id+""+name);}
publicstaticvoidmain(Stringargs[]){
Student10
s1=newStudent10(111,Mohammed");
Student10
s2=newStudent10(321,Shajahan");
s1.display();
s2.display();
}
Output:0 null
0
null
classStudent11{
intid;
Stringname;
Student11 (intid,Stringname){
this.id=id;
this.name=name;
}
voiddisplay()
{System.out.println(id+""+name);}
publicstaticvoidmain(Stringargs[]){
Student11s1=newStudent11(111,Moha
mmed");
Student11s2=newStudent11(321,Shaja
han");
s1.display();
s2.display();
Output 111
Mohammed
321 Shajahan
Explanation:What is super
Thesuperkeyword in java is
a reference variable that is
used to refer immediate
parent class object.
Whenever you create the
instance of subclass, an
instance of parent class is
created implicitly i.e. referred
classVehicle{
intspeed=50;
}
classBike3extendsVehicle{
intspeed=100;
voiddisplay(){
System.out.println(speed);//willprintspeed
ofBike
}
publicstaticvoidmain(Stringargs[]){
Bike3b=newBike3();
b.display();
}
}
Output:100
classBike4extendsVehicle{
intspeed=100;
voiddisplay(){
System.out.println(super.speed);//willprintspeedof
Vehiclenow
}
publicstaticvoidmain(Stringargs[]){
Bike4b=newBike4();
b.display();
}
}
Output:50
THANK YOU