[go: up one dir, main page]

100% found this document useful (1 vote)
431 views12 pages

This and Super Keyword in Java

This document provides an explanation and examples of the "this" and "super" keywords in Java. It defines "this" as a keyword that refers to the current object and is used to distinguish between class instance variables and local variables. It also defines "super" as a keyword used to refer to the parent class object and access parent class variables and methods. Examples are given showing the output differences when using and not using these keywords in class inheritance scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
431 views12 pages

This and Super Keyword in Java

This document provides an explanation and examples of the "this" and "super" keywords in Java. It defines "this" as a keyword that refers to the current object and is used to distinguish between class instance variables and local variables. It also defines "super" as a keyword used to refer to the parent class object and access parent class variables and methods. Examples are given showing the output differences when using and not using these keywords in class inheritance scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

THIS AND SUPER

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

Explanation: What isthis


thisis a keyword in Java. Which
can be used
insidemethodorconstructorof
class.
It(this)works as a reference to
current object whose method or
constructor is being invoked.
thiskeyword can be used to refer
any member of current object from

Uses of java this keyword


this keyword can be used to refer current
class instance variable.
this() can be used to invoke current class
constructor.
this keyword can be used to invoke
current class method (implicitly)
this can be passed as an argument in the
method call.
this can be passed as argument in the
constructor call.
this keyword can also be used to return
the current class instance.

Understanding the program


without THIS keyword

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

In the previous program,


parameter (formal arguments)
and instance variables are
same that is why we are using
this keyword to distinguish
between local variable and
instance variable.

Solution to the previous program by


THIS keyword

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

Program without super


keyword

classVehicle{
intspeed=50;
}
classBike3extendsVehicle{
intspeed=100;
voiddisplay(){
System.out.println(speed);//willprintspeed
ofBike
}
publicstaticvoidmain(Stringargs[]){
Bike3b=newBike3();
b.display();
}
}

Output:100

In the previous program Vehicle and


Bike both class have a common
property speed. Instance variable of
current class is referred by instance
by default, but we have to refer
parent class instance variable that
is why we use super keyword to
distinguish between parent class
instance variable and current class
instance variable

Solution to previous program by


super keyword
classVehicle{
intspeed=50;
}

classBike4extendsVehicle{
intspeed=100;

voiddisplay(){
System.out.println(super.speed);//willprintspeedof
Vehiclenow
}
publicstaticvoidmain(Stringargs[]){
Bike4b=newBike4();
b.display();

}
}

Output:50

THANK YOU

You might also like