What restriction there is no super reference in constructor?
Answers
Explanation:
➡️ What is the rule for a super reference in a constructor? It must be in the parent class' constructor. It must be the last line of the constructor in the child class. It must be the first line of the constructor in the child class.
If there is no super() in a constructor the compiler will add one implicitly. So both are constructor calls.So we can not have two statements as first statement, hence either we can call super() or we can call this() from the constructor, but not both. here if oyu use parameter constructor then there will be error.
"It seems this is what you are looking for"
What restriction is there on using the super reference in a constructor?
A. It can only be used in the parent's constructor.
B. Only one child class can use it.
C. It must be used in the last statement of the constructor.
D. It must be used in the first statement of the constructor.
Answer:
Correct option is :
D. It must be used in the first statement of the constructor.
It must be used in the first statement of the constructor there is no super reference in constructor.
Explanation:
The superclass function Object() { [native code] } with a matching parameter list is called with super(parameter list). Note that if a function Object() does not explicitly call a superclass function Object() , the Java compiler inserts a call to the superclass's no-argument function Object(). The constructor's first statement must include the letter t. Both the Car and Yugo classes have a run() method that was written by the programmer as part of the class definition. The first statement in a function Object() { [native code] } must be the function Object() { [native code] } call." As a result, you are not prevented from executing logic prior to the call to super. It simply prevents you from executing logic that cannot be contained within a single expression. Calling this() follows a similar set of rules.