Class Extension que

A

as4532

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
 
M

Mark Thomas

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
 
A

as4532

Hi Mark,
Yes File1.java compiles. Only File2.java throws an error saying-
"Cannot find the symbol" against File1.

Thanks
 
M

Mark Thomas

Hi Mark,
Yes File1.java compiles. Only File2.java throws an error saying-
"Cannot find the symbol" against File1.

Thanks
Post the full source - there's not enough information here.

Mark
 
A

as4532

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");
}
}
 
M

Mark Thomas

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:
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
 
A

as4532

Thanks for the reply Mark. I removed the word "extends". Now this is
the error message i am getting:

CustomerTest.java:11: cannot find symbol
symbol: class Customer
Location: class CustomerTest

Customer p=new Customer("fname","lname")

What i need to know is that how can i use the class which is in a
different .java file but same directory as my current .java file.





Mark said:
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
 
V

Venkatesh

U need to either set your classpath properly: Include "current
directory" in your classpath by saying
SET CLASSPATH=%CLASSPATH%;. in windows
or
CLASSPATH=$CLASSPATH:. in unix machines

Then run javac

Otherwise, u can compile both java files together, use
javac Customer.java CustomerTest.java
 
A

as4532

Hi Venkatesh,
Thanks a lot for the help. Classpath was the issue.. when i
compiled the 2 classes together it works fine. Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top