• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Superclass with no 'no-arg' constructor

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks...


How do I code the constructor for the subclass when I have a superclass which does not have a 'no-arg' constructor and I am not at liberty to insert one into it. Do have a look at the following code:

Animal



Horse


As it is there is compiler error due to the fact that there is no 'no-arg' constuctor in the super class. Hope someone can advise. Thanks.

regards
John




 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the horse constructor that you have supplied is a no-arg constructor. now the first line in any constructor will be a call to super or this. if you don't explicitly insert this or super call compiler will insert its no-arg super call which is like this super(). so when you wrote your horse constructor internally it will look like this




so in this case you have to explicitly insert super call which takes string parameter like this

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you created a constructor in superclass without args, you cannot call super() in subclass, because the default constructor is not created when we create a constructor with args. If you want to do of that code work, so add a constructor no args in superclass, like this:


 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

camilo lopes wrote:if you created a constructor in superclass without args, you cannot call super() in subclass, because the default constructor is not created when we create a constructor with args. If you want to do of that code work, so add a constructor no args in superclass, like this:




hi camilo. the original question says that "you don't have the liberty to insert no-arg constructor to Animal class" so you cannot modify Animal class. you have to insert super call with a string argument.
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gurpeet singh wrote:

camilo lopes wrote:if you created a constructor in superclass without args, you cannot call super() in subclass, because the default constructor is not created when we create a constructor with args. If you want to do of that code work, so add a constructor no args in superclass, like this:




hi camilo. the original question says that "you don't have the liberty to insert no-arg constructor to Animal class" so you cannot modify Animal class. you have to insert super call with a string argument.



oh yeah. I had forgotten of this part. thanks for remember me :0.
 
John Paterson
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys..

Thanks for the replies. Gurpeet Singh, your suggestion works. Thanks once again.

regards
John
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic