Tell me a way..........
Thank debasmita for the answer ,but the problem is I want to know why super() and this() are kept as only first statements.
- Soudhamini 4 years, 6 months ago
The parent class constructor requires to be called before the subclass' constructor. This will assure you that if you call some methods on the parent class in your constructor, the parent class has already been set up correctly.
So the this() and super() statements should be the first statements
In Java you can put a call to the parent constructor using the keyword
super
as the first line in a subclass constructor to initialize inherited fields. When to use super keyword :1) To access the data members of parent class when both parent and child class have member with same name
2) To explicitly call the no-arg and parameterized constructor of parent class) To access the method of parent class when child class has overridden that method.
Example :