• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Superclass with no 'no-arg' constructor

 
John Paterson
Ranch Hand
Posts: 171
  • 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




 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 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

 
camilo lopes
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
Fedora Java Netbeans IDE
  • 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: 171
  • 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
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic