aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Superclass with no 'no-arg' constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Superclass with no Watch "Superclass with no New topic
Author

Superclass with no 'no-arg' constructor

John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 121
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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Aug 08, 2007
Posts: 202
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:




Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Aug 08, 2007
Posts: 202
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

Joined: Mar 12, 2012
Posts: 121
Hi Guys..

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

regards
John
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Superclass with no 'no-arg' constructor