1

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 "

Zulukas
  • 1,180
  • 1
  • 15
  • 35
  • 1
    `this.file = file` are you assigning uninitialized `file` to itself? –  Nov 14 '14 at 20:00
  • I don't think you're showing quite enough of your intent to get good answers. Why is FileHandler a subclass of DataManager? Will a FileHandler instance ever be created other than by a DataManager instance? – Shaun Nov 14 '14 at 20:03

2 Answers2

4

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);
    }
}
Community
  • 1
  • 1
Mike R
  • 4,448
  • 3
  • 33
  • 40
  • Interessting answer. It is correct and this code solves the answer, but your text describes a different problem in OPs code :D. The mentioned (by OP) error is this line `new FileHandler(file)`, because `FileHandler` does not have such constructor. But still +1 worthy :D. – Tom Nov 14 '14 at 20:21
  • @Tom I noticed that line, but it doesn't make sense to create an instance of the child (FileHandler) from the Parent class (DataManager). You will end up with a circular dependency. – Mike R Nov 14 '14 at 20:24
  • That is right, but it is still the error OP reported. That is why I like your answer, you're not only solving that "little" problem. – Tom Nov 14 '14 at 20:25
0

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;
    }
}
subash
  • 3,116
  • 3
  • 18
  • 22