Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help: error: cannot find symbol

 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arslan Khan
Greenhorn
Posts: 18
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the following code in class A;

 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

no offense, but i think you must study constructors and instantiation again if you find suman's explanation difficult.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanku guys .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic