How are this and super used?

Answer Posted / qim2010

this() is used to invoke a constructor of the same class

super() is used to invoke a super class constructor and

Example of using this():
public Pet(int id) {
this.id = id; // “this” means this object
}
public Pet (int id, String type) {
this(id); // calls constructor public Pet(int id)
this.type = type; // ”this” means this object
}

Example of using super():

If a class called “SpecialPet” extends your “Pet” class then
you can
use the keyword “super” to invoke the superclass’s
constructor. E.g.
public SpecialPet(int id) {
super(id); //must be the very first statement in the
constructor.
}

To call a regular method in the super class use:
“super.myMethod( );”. This can be called at any line.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is set sorted in java?

514


How to compare two strings in java program?

669


What is treeset and treemap in java?

580


Difference between string, stringbuffer and stringbuilder?

565


What do you understand by classes in java?

555






What is the integer of 16?

537


What is the importance of static variable?

586


can any body body expalin best definitions & best real time exaples for opps concepts.

1837


FOR EXAMPLE WE R HAVING TWO LIST ELEMENTS ..BOTH LISTS CONTAINS ID,NAME,PLACE ..I NEED TO COMPARE BOTH IDS IN TWO LISTS,IF ID'S R SAME MEANS WE HAVE ADD THE DETAILS(LIKE NAME,PLACE) TO MAP...HOW IS POSSIBLE ?CAN ANY ONE SUGGEST?

2632


Explain the protected field modifier?

591


Which class cannot be a subclass in java?

539


Is java same as core java?

584


Does set allows null in java?

506


Which is illegal identifier in java?

587


What is difference between path and classpath variables?

566