This week's giveaway is in the Spring forum.
We're giving away four copies of liveProject: Protecting User Data with Spring Security and OAuth2 and have Laurentiu Spilca on-line!
See this thread for details.
Win a copy of liveProject: Protecting User Data with Spring Security and OAuth2 this week in the Spring forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

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: 72587
317
  • 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: 72587
317
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic