Click to See Complete Forum and Search --> : Compiling a class that extends File


Bnt
March 7th, 2002, 09:58 AM
Hi All,

I am having a problem compiling a class that extends java.io.File. It returns "cannot resolve symbol"
symbol : constructor File ()
location: class java.io.File
{
^

Lots of thanks to those who help me with this problem.

Bnt

dlorde
March 7th, 2002, 11:32 AM
Can you post up the relevant code (including package declaration, imports, and the constructor, at least)?

Dave

Bnt
March 8th, 2002, 02:06 AM
Thanks Dave,

Here is the code:


package EMail_Backup;

import java.io.File;

public class DBFile extends File
{
private File dbFile;

public DBFile(String pathStr)
{
dbFile = new File(pathStr);
}
}




Thanks again
Byron

dlorde
March 8th, 2002, 06:01 AM
OK, the problem is that you have provided a non-default constructor for your subclass. As in C++, when you do this in Java, it will hide any other superclass constructors. Since you aren't calling the superclass File(String xxx) constructor from your subclass constructor to construct the superclass (File) part of the object, the compiler tries to call the default (no args) superclass constructor (the superclass part must be constructed somehow). Unfortunately there is no-args default constructor in the File class.

The solution is to call the superclass constructor from your own: public DBFile(String pathStr) {
super(pathStr);
dbFile = new File(pathStr); // What is this for?
}

Note that the call to super must be on the first line.
This will fix the compiler error, but points out an oddity in your design. Why extend the File class but then use a File object as a data member and ignore the superclass? Usually you either extend the superclass or aggregate an object of that class, not both.

Why are you doing both? What is the data member dbFile for?

Dave

Bnt
March 8th, 2002, 07:11 AM
Hi Dave,

I owe you two thanks:
Firstly for fixing my original problem,
Secondly for pointing out that I dont need to create a file object in a class that extends file.
I have removed that now.

Keep Well
Byron