1

I have the following classes inheritance structure:

public class Document {}
public class AuditProgramDocument extends Document {}
public class CaseFilePart {
    private Document document;
    public Document getDocument() {
        return document;
    }
}
public class MyClass {
    public boolean canDeleteDocument(CaseFilePart selectedCFP) {
        ...//somelogic
        if (selectedCFP.getDocument() instanceof AuditProgramDocument) {
            System.out.println("instance");
        }
    }
}

In debugger, I clearly see that, selectedCFP.getDocument() returns AuditProgramDocument. But If statement doesn't evaluated.

My test:

System.out.println("2.1 " + selectedCFP.getDocument().getClassName());
System.out.println("2.2 " + selectedCFP.getDocument().getClass().getName());
System.out.println("2.3 " + selectedCFP.getDocument().getClass().getCanonicalName());
System.out.println("2.4 " + selectedCFP.getDocument().getClass().getSimpleName());

Results in output:
2.1 auditProgramDocument
2.2 eu.erp.crams.cmg.bom.document.Document$$_javassist_79_
2.3 eu.erp.crams.cmg.bom.document.Document$$_javassist_79_
2.4 Document$$_javassist_79_

10
  • 7
    "Java instanceof doesn't work" Yes, it does. Commented Jun 13, 2016 at 12:09
  • 7
    Try to print selectedCFP.getDocument() before the if. In general: stuff works; if things are broken, then in 99.999999% of time, it is your code. Commented Jun 13, 2016 at 12:09
  • 1
    @Beezy Is 2.1 actually auditProgramDocument (lowercase a), or is it AuditProgramDocument (uppercase A)? Commented Jun 13, 2016 at 12:21
  • 2
    It IS a different class... 2.2 eu.erp.crams.cmg.bom.document.Document$$_javassist_79_ Commented Jun 13, 2016 at 12:28
  • 2
    The output of getClassName() means almost nothing. It’s not a standard Java method, it’s a method you wrote. Perhaps you should include the code for that method in your question. The code for the toString method of Document and its subclasses might also be useful. Commented Jun 13, 2016 at 14:22

3 Answers 3

3

So, to be sure we have the following rules:

AuditProgramDocument is Document

CaseFilePart has Document

When we write the code:

Document document = new AuditProgramDocument();
if (document instanceof Document) //true
if (document instanceof AuditProgramDocument) //true

But, when we write the code:

Document document = someVar.getDocument();
if (document instanceof Document) //true
if (document instanceof AuditProgramDocuemtn) //false

The last statement is false because Document is not AuditProgramDocument. Because the getDocument() method always returns us reference to Document, not subtype

Sign up to request clarification or add additional context in comments.

Comments

0

1)-:In side CaseFilePart class you are creating reference variable of Document class not object of document class.

You should return object of Document class not reference.

2)-:AuditProgramDocument class is child class of Document class we can not down cast it.

3)-:that is why selectedCFP.getDocument() not instanceof AuditProgramDocument

Comments

0

Try this code... (commented out, but included your original tests)

public class MyClass {
    public boolean canDeleteDocument(Document doc) {
        return (doc instanceof AuditProgramDocument);
    }

    public static void main(String[] args) {
        //System.out.println("2.1 " + selectedCFP.getDocument().getClassName());//DOES NOT COMPILE
        //Not sure about these tests....
        //System.out.println("2.2 " + selectedCFP.getDocument().getClass().getName());
        //System.out.println("2.3 " + selectedCFP.getDocument().getClass().getCanonicalName());
        //System.out.println("2.4 " + selectedCFP.getDocument().getClass().getSimpleName());

        MyClass mc = new MyClass();
        CaseFilePart cfp = new CaseFilePart();

        //TEST 1
        cfp.setDocument(new AuditProgramDocument());
        boolean canDelete = mc.canDeleteDocument(cfp.getDocument());
        System.out.println("Can we delete " + cfp.getDocument().getClass() + " " + canDelete);

        //TEST 2
        cfp.setDocument(new SomeOtherDocument());
        canDelete = mc.canDeleteDocument(cfp.getDocument());
        System.out.println("Can we delete " + cfp.getDocument().getClass() + " " + canDelete);
    }

}

//Put all these in the same file to simplify the answer
class Document {}
class AuditProgramDocument extends Document {}
//Created 2nd Document implementation for illustration
class SomeOtherDocument extends Document {}
class CaseFilePart {
    private Document document;
    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.