wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes help: error: cannot find symbol Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help: error: cannot find symbol" Watch "help: error: cannot find symbol" New topic
Author

help: error: cannot find symbol

Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Hello dear all,

When I do compile the following code, it shows the error

E:\JAVA>javac Ext.java
Ext.java:7: cannot find symbol
symbol : constructor A()
location: class A
class B extends A
^
1 error




Can anybody explain me why this error happened?


thanks
Jyo
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Explicitly specify a default constructor in A. Constructors are chained and when you call a constructor of a sub class the super class constructor is run first. Here, As you haven't specified any default constructor in A and yes the compiler won't put any too.

The other alternative is make a default constructor in B and put the first line as super(3) or any int value in super.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Arslan Khan
Greenhorn

Joined: Jul 29, 2009
Posts: 18

Add the following code in class A;



If you can't make it good, at least make it look good.
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Nitish Bangera wrote: As you haven't specified any default constructor in A and yes the compiler won't put any too.



But the class has an implicit constructor ?

Or its true for just a simple class and not for the class hirarchy?

Also if I write following it gets compile.
Without specifying any constructor in A?



Please help!
Suman Poluri
Greenhorn

Joined: Aug 04, 2009
Posts: 7
There are a few things to keep in mind
1. The compiler WILL put a no arg constructor if you do not supply ANY constructor.
2. If you DO NOT supply a no-arg constructor but DO supply a constructor with args, the compiler WILL NOT take up the responsibility for the no-arg constructor.
3. The compiler implicitly puts in a constructor with 'super()' in the subclass if you do not provide one for the subclass explicitly.
4. When you instantiate a sublass, the constructors of ALL it's superclasses are invoked automatically (via super()), all the way up to the great-grand-dad 'Object'.

(Please note: The caps in the text above are just to highlight the important aspects, no offense meant.)

So in your first example, the class A had a contructor with integer args but did not have a no-arg constructor. When you compiled A.java, the compiler did not put in a no-arg constructor.

Think of it this way. 'B extends A' means that when you try to instantiate B, the constrcutor of B is invoked, which inturn invokes the constructor of it's superclass (i.e. 'A'). But 'A' has only one constructor that takes an integer argument (remember the compiler doesnt put a no-arg constructor in this case). But how is that possible if you dont supply anything to A's constrcutor? That was the reason the compiler raised an objection.

Incase you're wondering there's two ways this would work

First way:



Second Way:




Hope this helps. Enjoy!
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Thanku for this fantastic explanation.
I am very clear with the concept now.
thanku .

But can you please explain the following??

Suman Poluri wrote:'B extends A' means that when you try to instantiate B, the constrcutor of B is invoked,

I guess instantiate means
How B extends A would be an instantiate???
So there is no instance of B called still it calls super()???

that means while compiling it invokes constructor & not at ??
the code below just compiles as above code throwing error.

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Look when you say B extends A means now we can inherit all the things of A into B but Constructors are never inherited. Also if you create an instance of B say B b = new B(); N no constructor is written over there then the compiler puts a default constructor and the first line of the constructor is always to super(). It is implicit. It is either super() or this(). this() is explicit representation but super() is implicitly put. So when you instantiate an instance of the sub class the Constructor of the sub class is invoked which calls the super class's default constructor which is not there only. Why its not there because the you have already written down a constructor and so the compiler will not put a default no arg constructor. So the call to super() from the sub class constructor fails. So the B instance won't be created because its constructor execution fails.

I hope i have not complicated things.
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
hey,

no offense, but i think you must study constructors and instantiation again if you find suman's explanation difficult.
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
thanku guys .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help: error: cannot find symbol