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
What are methods and how are they defined?
What is immutability in java?
Are the imports checked for validity at compile time? Will the code containing an import such as java.lang.abcd compile?
How to sort array of 0 and 1 in java?
What is a heavyweight component?
Can we declare a constructor as final?
Can we have two main methods in a java class?
Which is better 64 bit or 32 bit?
Is array serializable java?
Where can I find jdk in my computer?
How do you declare an array in java?
How do you check if a character in a string is a digit or letter?
What is default size of arraylist in java?
Is null in java?
How do you sort objects in java?