Home>

This is a rudimentary question.
I want to be able to write a simple code likeinstanceofas a way to determine whether an instance is an instance generated from a specific class.

Error message

An instance e of class E that inherits class C isfalsewhen compared with classes A, B, C, and D, andtrue(that is, unlikeinstanceof, it returnstrueonly when the class matches).

Applicable source code
class A {}
class B extends A {}
class C extends B {}
class D extends C {}
class E extends C {}
public class Main {
    public static void main (String [] args) {
        E e = new E ();
        System.out.println (e.getClass (). Equals (A.class));//->false
        System.out.println (e.getClass (). Equals (B.class));//->false
        System.out.println (e.getClass (). Equals (C.class));//->false
        System.out.println (e.getClass (). Equals (D.class));//->false
        System.out.println (e.getClass (). Equals (E.class));//->true
    }
}

I originally usedinstanceof, but
It has become necessary totrueonly when the situation has changed and the instance matches the specified class.
I want the parent class to befalseproperly.

I came up with the source code above.
However, I tried to see if it could be written more simply asinstanceof.
I'm asking you because I can't find it.

OBJ09-J. Compare class names To compare classes instead of , there was a way to check if objects x and y were the same class. However, there was no way to determine if object x was an instance created from class Y.

If i have a better way to write than

instanceof, please let me know.
Thank you.

Supplemental information (FW/tool version etc.)

OS: Windows 10
Java: dokojava v2