Tutorial :Cannot find constructor File() in java.io.File


This is probably obvious, so bear with me.

YES, I KNOW THAT java.io.File has no default constructor.
The problem is that When I try to extend java.io.File, it says "Cannot find constructor File() in java.io.File" even though I am overriding the default constructor in java.lang.Object.

Here is my code:

import java.io.File;  public class AbsRelFile  extends File {      File f;      private AbsRelFile(){        }  }  

This gives me an error, even though I am overriding the constructor.

NOTE: This class is not finished. Don't make a comment about why wouldn't I need this or a comment about how this class is useless. I just started writing it before I got this Error.


Because you didn't make an explicit call to super(...) in your default constructor, it is implicitly attempting to call the default constructor for the super class, which, as you point out, doesn't exist in this case (the case of super being a File). The solution to your problem is to make a call to the super constructor in your default AbsRelFile() constructor. If you wan't to provide a default constructor for your class, you're going to need to call super(...) with some default values.


When you define a constructor, Java inserts an implicit call to the super constructor as the very first line of the constructor. So your constructor is equivalent to:

private AbsRelFile(){      super();  }  

Since there is no default constructor in the super class File, it gives an error. To fix this, you need to place an explicit call to the super class constructor as the first line:

private AbsRelFile(){      super("fileName");  }  

Most probably, you'll have to define some suitable parameters for AbsRelFile constructor too which you can pass to super call.

On another note, constructors cannot be overridden. So it is wrong to say that you're overriding the Object class constructor. You're simply defining a constrcutor for AbsRelFile class.


Constructors, by default, call the default constructor of the super-class if you don't make a super constructor call yourself.

To avoid this, make a call to an actually-defined constructor of File.


Java automatically puts in a call to super() in your empty constructor, which is why you get the error.


The problem is that your AbsRelFile constructor is trying to call the no-args constructor of File. What you have written is equivalent to

private AbsRelFile() {      super();  }  

You need to make sure that you explicitly invoke one of the File constructors that does exist. For example:

private AbsRelFile() {      super("dummy");  }  

Obviously, you will need to figure out a safe / harmless / appropriate superclass constructor and arguments to use for your particular use-case. (I haven't a clue what an AbsRefFile is really supposed to be ... so I cannot advise on that.)

Aside - you don't "override" constructors. Constructors are never inherited in Java, so overriding simply doesn't apply here. Instead you declare constructors in the subclass, and have them chain to the appropriate constructors in the immediate superclass via an explicit super(...) call ... or the implicit one the Java inserts by default.


First of all, I hope your field "File f" is not related to trying to access the superclass, but something to do with 'Rel' or 'Abs'..

The other posters have correctly pointed out that your implicit default constructor (AbsRelfile()) will attempt to call super() - which does not exist. So the only solution is to make a constructor that passes down some valid arguments.

If you are attempting to 'wrap' the whole java.util.File class (like when making your own Exception) you should probably provide a wrapper for each of the original constructors. Modern IDEs like Eclipse should have this a right-click away.

Note that File does not require that the given filename exists, in particular it does not exist when you want to do operations like file.mkdir().

If you need an actual, temporary file to work with, you could always do something like:

public class AbsRelFile() {       public AbsRelFile() {             super(File.createTempFile("AbsRelFile", "tmp").getAbsolutePath());       }  }  

.. but I'm puzzled as to why you want to subclass File in the first place.


To explain WHY in one line: When you define a constructor with a parameter (as in the File class), Java compiler WILL NOT generate the default constructor for you.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »