Hi All,
Welcome
to Java-recent.
In
this post we will discuss about usage of “super” and
“this” key words .
First
we will discuss about super keyword
super:-
- Is a key word in Java primarily used to invoke immediate super /parent class methods
- used to invoke immediate super/parent class constructor
- used to invoke immediate super/parent class instance variable
We
will use same old classes Animal.java , Lion.java
First
lets have a look at the code
Animal.java
public class Animal {
private
String name="java-recent";
//Parent
class parameterized constructor
public
Animal(String animal)
{
System.out.println(animal);
}
//Display
method
public
void
display()
{
System.out.println("in
animal-a parent class");
}
}
Lion.java
public
class Lion extends
Animal{
public
Lion()
{
super("From
lion");
System.out.println("In
child class Lion constructor");
}
public
void
display()
{
System.out.println("i
am an Elephant-a child class");
//calling
Animal class display method
super.display();
//Invoking
super class instance report
System.out.println(super.name);
}
}
Tester.java
public
class Tester
{
public
static
void
main(String[] args)
{
//creating
Lion object
Lion
lion=new
Lion();
lion.display();
}
}
output
From
Lion
In
child class Lion constructor
i
am an Elephant-a child class
in
animal-a parent class
java-recent
Example
:-
- Invoking of parent class super method using super.display() in Lion.java;output for it is in animal-a parent class
- Invoking super class constructorsuper(“From lion”); in Lion constructoroutput for this is “From Lion” statement
- Invoking super class instance variablesuper.name is used to access super class instance variableoutput for this is “java-recent”
Note:-
- super statement in child class constructor should be the first statement
- “ If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.
Object
does have such a constructor, so ifObject
is the only superclass, there is no problem. “ Taken form http://docs.oracle.com/javase/tutorial/java/IandI/super.html
Now
we will discuss about this key word
“this”
- this key word is used to refer members of same class.
- this is reference to current object
We
will see the importance of this key word in the following examples.
public
class
ThisKey
{
private
int
height;
private
int
width;
public
ThisKey(int
height,int
width )
{
height=height;
width=width;
}
//Getters
and setters
public
int
getHeight() {
return
height;
}
public
void
setHeight(int
height) {
this.height
= height;
}
public
int
getWidth() {
return
width;
}
public
void
setWidth(int
width) {
this.width
= width;
}
public
static
void
main(String[] args)
{
ThisKey
thisKey=new
ThisKey(10,20);
System.out.println("height
is "+thisKey.getHeight());
System.out.println("width
is "+thisKey.getWidth());
}
}
Output:-
What
would be the output values for height and width 10,20 ?
No
the output will be zero and zero. How ?
In
the parametrized constructor we are using height=height and
width=width, according to variable scope local variables will have
more importance,so the assignment refers to local values,instance
values are not updated.
height
is 0
width
is 0
Now
if we want to assign values for instance variable we need to use this
key word.
Just
change the code of constructor as below
public
ThisKey(int
height,int
width )
{
this.height=height;
this.width=width;
}
Output:-
As
we has used this key word,the value will be assigned to instance
variables
height
is 10
width
is 20
Similarly
we can use this() for calling self constructors/methods
Examples:-
public
ThisKey(int
height,int
width )
{
//calling self constructor
this();
this.height=height;
this.width=width;
}
//default
constructor
public
ThisKey()
{
}
Happy
Learning
Please
provide your valuable comments on this article and share it across
your network.
No comments:
Post a Comment