overriding class

Hi,

I wanted to add an extra metod to java.io.File class. So a created my own File class within my package that should override java.io.File class. Looks like this:

publicclass Fileextends java.io.File{

/** Constructor */

public File(String pathname){

super(pathname);

}

public File(String parent, String child){

super(parent,child);

}

public File(java.io.File parent, String child){

super(parent,child);

}

public File(URI uri){

super(uri);

}

public String getExtension(){

String name = getName();

int index = name.lastIndexOf(".");

if (index==-1 || index==0){

return"";

}

String extension = name.substring(index+1);

return extension;

}

}

The problem is, that when i want to use it in a different calss within my package it gives me an Error incompatible types. Part of the other class:

privatestaticvoid indexDirectory(IndexWriter writer, File dir)

throws IOException{

File[] files = dir.listFiles();// Error:incompatible types

File f=null;

for (int i=0; i<files.length; i++){

f = files[i];

String fileExtension = f.getExtension();

if (f.isDirectory()){

indexDirectory(writer, f);

}elseif (indexFileTypes.contains(fileExtension)){

indexFile(writer, f);

}

}

}

Im a beginner in Java so It can be something stupid. But i thaught that subclass should inherit all the methods and also that i can assign the superclass type to the subclass type as the subclass is the same as superclass except it only has one more method.

Can somebody help me with this?

Thanks>

[3283 byte] By [bontimea] at [2007-10-2 5:39:33]
# 1

I recommend that you rename your class to something besides File, like MyFile.File[] files = dir.listFiles();

This line declares an array of your File type then attempts to call listFiles(). But the listFiles() method returns an array of type java.io.File array. There is no inheritance relationship between a java.io.File array and your File array, except Object. So the types are incompatible.

It might be best to declare the array as java.io.File[].

You should also be able to override the listFiles() method and inside the new method, take the elements of a java.io.File array and put them into your File array.

atmguya at 2007-7-16 1:49:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Thanks. Now I understand why it shows error. But even if i rename the class to MyFile its inherited method listFiles will still return File array instead of MyFile array. How do I fix this? How do i define MyFile array? I probably don't understand the way how an array is defined/created.

Thanks again.

bontimea at 2007-7-16 1:49:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> Thanks. Now I understand why it shows error. But even

> if i rename the class to MyFile its inherited method

> listFiles will still return File array instead of

> MyFile array. How do I fix this? How do i define

> MyFile array? I probably don't understand the way how

> an array is defined/created.

> Thanks again.

I would probably add another method, "listMyFiles()" and have this method create a MyFile[] and fill it with MyFiles. At least it looks like you expect to be able to call getExtension() on the objects in the array, so they must be MyFiles.

atmguya at 2007-7-16 1:49:52 > top of Java-index,Other Topics,Patterns & OO Design...