• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the error in subclass constructor

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic