I am very very new to java and trying to extend File class to add some more methods and properties to it with below class
import java.io.File;
public class RecordingFile extends File {
private static final long serialVersionUID = -4774703382591639948L;
String RecordingNameFormatted;
String RecordingDurationFormatted;
String RecordingSizeFormatted;
String RecordingDateFormatted;
String RecordingBitrateFormatted;
public RecordingFile(String path) {
super(path);
File f = new File(path);
RecordingNameFormatted = f.getName().replace(".mp3", "");
RecordingDurationFormatted = Utils.millisecondsToHours(Utils.getMp3DurationPureJava(f));
RecordingSizeFormatted = Utils.humanReadableByteCount(f.length(), true);
RecordingDateFormatted = Utils.longToDate(f.lastModified());
RecordingBitrateFormatted = Utils.getBitratePureJava(f) +"Kbps";
}
}
Above doesn’t work properly. I am not even sure if
File f = new File(path);
supposed to be there. For example when I try this
ArrayList<RecordingFile> ret = new ArrayList<RecordingFile>();
RecordingFile filesDir = new RecordingFile(path);
if (!filesDir.exists()) {
filesDir.mkdirs();
}
RecordingFile[] list = filesDir.listFiles();
Eclipse tells me that “Type mismatch: cannot convert from File[] to RecordingFile[]”
How can i just extend File class by adding couple more properties/methods to it?
None of the builtin things know about your class, so they can’t possibly return your new types. what i think you’re looking for is probably extension methods which don’t exist in Java yet.
Java equivalent to C# extension methods
In order to use your stuff, you’d have to make a method that, given any collection of File objects, returns your new type. You’d then have to call that everywhere you want to use your type instead.
so that line would look like
i leave the implementation of convertToRecordingFiles as an excercise 🙂