aspose file tools*
The moose likes Beginning Java and the fly likes inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritance" Watch "inheritance" New topic
Author

inheritance

kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87




cannot find symbol
symbol : constructor Inherit()
location: class Inherit
public InheritB(int a,int b,int c) {


Why do I get this error?
thank you
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

This will work:



Why do I get this error?


you will have to define a call to super explicitly. Compiler is trying to use super(); in your case. But you super class does not have non parametrized constructor.


Pawan Chopra
SCJP - DuMmIeS mInD
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87

thank you
birun takhellambam
Greenhorn

Joined: Mar 06, 2010
Posts: 4
add the following constructor to Inherit.java, :

//need a default constructor
public Inherit()
{}
//end of constructor
.....
try it.


birun
Rahul Kurup
Greenhorn

Joined: Dec 11, 2009
Posts: 9
When you create object for InheritB(child class) a constructor for its parent class will be called implicitly. By default, the compiler looks for the zero argument constructor in the parent class. Now you have explicitly provided a constructor for Inherit class. If you hadn't, then compiler would have provided a zero argument constructor for you.
So there are 2 solutions for the program to work as described by the previous replies. One is, provide a zero argument constructor in Inherit class in addition to the already existing constructor. Second one, is by telling compiler not to look for the default zero argument constructor in parent class and call the one which you have created(using super). Since i and j will be initialized using super call, you don't have to explicitly initialize them in InheritB constructor.


Rahul


Rahul J.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
If you need parameters for your constructor in the superclass, then don't add a no-parameter constructor. Put the appropriate arguments in the super() call in the subclass constructor. If you search, you can find some old threads which are helpful: here are a few which might or might not help 1, 2, 3.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance