• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the error in subclass constructor  RSS feed

 
Rajiv KumarRai
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The above code shows error in subclass constructor with the following error message
"Implicit super constructor Test1() is undefined. Must implicitly invoke another constructor."

Please help me understand the cause of the error.
Thanks
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call a constructor, it must first go to the superclass' constructor. You didn't write super(...);, so the compiler will attempt to add super();
But your superclass does not have a no‑args constructor, so super(); cannot match a constructor call. So the compiler can't complete compilation.

You must add a super(int); statement to your subclass' constructor.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajiv,
In inheritance and parameterized contructor, there are few rules.
Rule 1 : If Derived class contructor is parameterless, there has to be 1 parameterless constructor in Base class
If you dont want to add a parameterless constructor in Base class, then
Rule 2 : Call the contructor of Base class in the Derived class contructor explicitly.
NOTE : The call must be the first statement in the Derived class constructor.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is confusing; I think you know what the rules are and have expressed them badly.

By the way: say superclass and subclass. Only the C# people say Base and Derived, even though Base class and Derived class are probably better terms than superclass and subclass.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!