Extending java.io.File

U

ufan100

Hi, Is it possible to extend java.io.File?

javac (through Netbeans) gives me an error:

cannot find symbol:
symbol : constructor File()
location: class java.io.File
class Dir extends java.io.File {
1 error
BUILD FAILED (total time: 5 seconds)

Cheers
Lucas
 
E

Eric Sosman

Hi, Is it possible to extend java.io.File?

I see no reason why not.
javac (through Netbeans) gives me an error:

cannot find symbol:
symbol : constructor File()
location: class java.io.File
class Dir extends java.io.File {
1 error
BUILD FAILED (total time: 5 seconds)

The problem isn't with File, but with your subclass.
You are attempting to call the no-argument constructor of
File, but File doesn't have such a constructor.

You may not have written an explicit call on File's
constructor, either as `new File()' or as `super()' in a
FunnyFile constructor, but remember: a subclass' constructor
cannot start doing its job until its superclass' constructor
has finished. The job of the FunnyFile constructor is to
take an existing File and specialize it; the File itself
is built by File's own constructor. This "constructor
chaining" is so important that if you do not call `super()'
explicitly (perhaps with arguments), the compiler inserts a
no-argument `super()' call on your behalf. And if the
superclass doesn't have a no-argument constructor, as File
doesn't, the no-argument `super()' call won't compile.

Why doesn't the compiler call one of the constructors
File actually has, instead of calling one that doesn't
exist? Well, which of them should it call, and with what
argument values? The compiler cannot read your mind.
 
U

ufan100

I see no reason why not.



The problem isn't with File, but with your subclass.
You are attempting to call the no-argument constructor of
File, but File doesn't have such a constructor.

You may not have written an explicit call on File's
constructor, either as `new File()' or as `super()' in a
FunnyFile constructor, but remember: a subclass' constructor
cannot start doing its job until its superclass' constructor
has finished. The job of the FunnyFile constructor is to
take an existing File and specialize it; the File itself
is built by File's own constructor. This "constructor
chaining" is so important that if you do not call `super()'
explicitly (perhaps with arguments), the compiler inserts a
no-argument `super()' call on your behalf. And if the
superclass doesn't have a no-argument constructor, as File
doesn't, the no-argument `super()' call won't compile.

Why doesn't the compiler call one of the constructors
File actually has, instead of calling one that doesn't
exist? Well, which of them should it call, and with what
argument values? The compiler cannot read your mind.

Thank you for taking the time ... I've clearly got some reading to do
 
R

Roedy Green

cannot find symbol:
symbol : constructor File()
location: class java.io.File
class Dir extends java.io.File {
1 error
BUILD FAILED (total time: 5 seconds)

your Dir constructors must as the very first thing call super(...)
some constructor of File.

Also check that File is not final.
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top