Java 1.6. I added a class to include some methods. Now I would like to use an extended class instead of a base class. However, classes that can use the base class cannot “recognize” the extended class. What is a (recommended) fix?
I know that it has been asked many times in different tastes, but I can’t get it!
Example. Extend the SAMRecord class and use the SAMRecordExt afterword:
public class SAMRecordExt extends SAMRecord{ public SAMRecordExt(SAMFileHeader header) { super(header); } }
Now that this works:
SAMRecord rec= sam.iterator().next();
This gives me a compilation error
SAMRecordExt recext= sam.iterator().next(); >>> Type mismatch: cannot convert from SAMRecord to SAMRecordExt
No wonder this doesn't work either (runtime error):
SAMRecordExt recext= (SAMRecordExt) sam.iterator().next(); >>> Exception in thread "main" java.lang.ClassCastException: htsjdk.samtools.SAMRecord cannot be cast to markDupsByStartEnd.SAMRecordExt at markDupsByStartEnd.Main.main(Main.java:96)
How can I make the extended class work where the base class worked?
EDIT : In more detail about the classes that I use. sam object comes from
SamReaderFactory sf = SamReaderFactory.makeDefault(); SamReader sam= sf.open(new File(insam));
Full documentation https://samtools.imtqy.com/htsjdk/javadoc/htsjdk/index.html
source share