java - In issue with ClassCastException while extending File class -


i've small difficult me find out problem concerning extending file class own myfile class. point make method recursively find files in folder tree. , i've stumbled upon problem classcastexception @ point of calling listfilesrecurse() of type myfile on files casted myfile. i've been thinking approch , sadly seems quite reasonable... i'd grateful of guys helped me.

public class myfile extends file {      public myfile(string  s) {         super(s);     }      public list <file> listfilesrecurse() {         list <file> finallist = new arraylist<file>();          file [] tab =   this.listfiles();         (file tab1 : tab) {             if (tab1.isfile()) {                 finallist.add(tab1);             } else if (tab1.isdirectory()) {                 ((myfile) tab1).listfilesrecurse();             }         }      return finallist;     } } 

i didn't realize rules of inheritance , i've came working version.

public class myfile {  string path; public myfile (string path) {     this.path = path; }  list <file> list = new arraylist<file> (); list <string> namelist = new arraylist <string> ();   list <file> listfilesrecusive() {      file f = new file(path);      file [] files = f.listfiles();      (file fil : files) {          string subfile = fil.getname();          if (fil.isfile()) {         list.add(fil);         namelist.add(fil.getname());     } else if (fil.isdirectory()) {         this.path = fil.tostring();         this.listfilesrecusive();      }     }     return list; } 

what you're doing doesn't make sense.

when do

this.listfiles(); 

it returning real file instances base class method. base class has no idea made derived class, , file instances returns cannot possibly casted derived class.

possible solution

you create myfile existing files constructor, casting out of question. so, create constructor in myfile takes file instance, , use instead of cast.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -