Reputation: 1280
I have a main class, DataManager which has a subclass FileHandler which extends DataManager.
public class DataManager{
protected File file;
private FileHandler fileHandler;
public DataManager(File fileIn) {
this.file = fileIn;
fileHandler = new FileHandler(file);
}
//other class stuff}
the other class:
public class FileHandler extends DataManager {
private File file;
public FileHandler() {
this.file = file;
}
//other class stuff }
I'm getting issues with an error stating that the constructor cannot be applied given types. This is my first time working with inheritance in Java and this issue isn't being very helpful as towards what it wants from me.
Here is a more specific version of what I'm getting from NetBeans...
"constructor DataManager in class DataManager cannot be applied to given types; required: File found: no arguments reason: actual and formal arguments lists differ in length "
Upvotes: 1
Views: 1177
Reputation: 4538
FileHandler() is implicitly calling its parent's constructor: super().
super() is expecting to find a constructor that takes no arguments. However you only have 1 constructor on the parent that requires a file.
Refer to the following:
To fix your error call: super(file)
Your code should be:
import java.io.File;
public class DataManager {
protected File file;
public DataManager(File fileIn) {
this.file = fileIn;
}
}
// -------------
import java.io.File;
public class FileHandler extends DataManager {
public FileHandler(File file) {
super(file);
}
}
Upvotes: 4
Reputation: 3140
create a another constructor.. beacause new FileHandler(file)
compiler know one argument constructor doesn't exist in FileHandler
public class FileHandler extends DataManager {
private File file;
public FileHandler() {
}
public FileHandler(File file) {
this.file = file;
}
}
Upvotes: 0