Mark, Here is the source. Customer.java compiles while
CustomerTest.java doesn't. Thanks
//Customer.java
public class Customer{
private String fname, lname;
public static void main(String[] args){
}
public Customer(String firstname, String lastname){
this.fname=firstname;
this.lname=lastname;
}
}
//CustomerTest.java
public class CustomerTest extends Customer{
public static void main(String[] args){
Customer p=new Customer("fname","lname");
}
}
Mark said:
(e-mail address removed) wrote:
Hi Mark,
Yes File1.java compiles. Only File2.java throws an error saying-
"Cannot find the symbol" against File1.
Thanks
Mark Thomas wrote:
(e-mail address removed) wrote:
Hi all,
I have a newbie question about extending classes in Java. I have 2
files File1.java and File2.java. File1.java is the base class and
File2.java extends it.
//File2.java
public class File2 extends File1 {
....
}
Both files are in the same folder in my C:\. I get the error saying
Cannot find symbol File1 when i try to compile File2.java. What should
i do here to use an existing class in another java program when both
.java files are present in the same folder.
Thanks
A
Has File1.java compiled cleanly?
Note that these are not separate java programs - just two java classes.
Even calling them Files tends to be misleading.
Mark
Post the full source - there's not enough information here.
Mark
Ah! You misquoted the error message - it doesn't say "Cannot find
symbol File1 (or Customer)" rather "cannot find symbol : constructor
Customer()". Your CustomerTest class doesn't define a constructor -
when that happens, Java provides you with a default no-argument
constructor. Any constructor always calls the corresponding constructor
in its superclass, in this case that would be a no-argument constructor
in Customer - and you haven't got one! Hence the error message. Note
that as soon as you provide a constructor for a class, as you have for
Customer, you lose the default constructor.
Here the is no reason for CustomerTest to extend Customer. It is not a
subclass (there is no way that a CustomerTest is-a-kind-of Customer),
but a class that uses Customer. So remove the 'extends Customer' and
all will be well.
Always give the actual error message that is causing you problems -
having read it first!
Mark