What is the difference between this() and super()

Options
- super() constructor is invoked within a method of a class while this() constructor is used within the constructor of the sub class
- this() constructor is invoked outside a method of a class while super() constructor is used within the constructor of the sub class
- this() constructor is invoked within a method of a class while super() constructor is used within the constructor of the sub class
- this() constructor is invoked within a method of a class while super() constructor is used outside the constructor of the sub class


CORRECT ANSWER : this() constructor is invoked within a method of a class while super() constructor is used within the constructor of the sub class

Discussion Board
this and super

The main difference between this() and super() that this() represent current instance of a class, while super() represent current instance of parent class.

Constructor chaining is achieved by using this() and super() keywords. this() is used to call no argument constructor of same class, while super() is used to call no argument or default constructor of parent class.

this() can be used in InnerClasses and Outer.this and Outer.super can be used to get current instance of Outer class and it's parent in Java.

The difference are as follows:

1) this is used in context of the class you are working on, while super is used to refer current instance of parent class.

2) Every constructor by default calls super(), which is a call to no argument constructor of parent class.

Rohit Sharma 08-7-2014 08:51 AM

validate answer

The given answer is wrong. Give a try.

This is better option -this() constructor is invoked outside a method of a class while super() constructor is used within the constructor of the sub class

vineet 11-23-2013 11:55 PM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement