2

For example,

public class A{...}

public class B extends A{...}

A a;
if(...) a = new A();
else a = new B();

Then, I want check if a is A or B. Is there any way to do this?

3
  • @Eran Because a can be either A or B. I want to check after the if condition, what instance of a is. Commented May 7, 2017 at 7:01
  • 2
    there is instanceof method in java to verify whether certain obj is a instance of a class. Commented May 7, 2017 at 7:02
  • I thought you were asking what to put instead of the .... If you want to know the type after the condition, instanceof is what you need. Commented May 7, 2017 at 7:03

4 Answers 4

4

Check the type of object with instanceof take a look at the following

if(a instanceof B) {
    // the object is of sub class
} else {
    // the object is of super class
}
Sign up to request clarification or add additional context in comments.

1 Comment

If A a = new B(), will instanceof A be confused? is the output true? becuase B is derived from A.
3

you can check whether an instance is a type of a class by following way

 if (a instanceof A) {
    //...
} else {
    //...
}

5 Comments

Oh, thanks! So, instanceof won't be confused even if B is subclass of A?
But if a refers to a B object, then a instanceof B is still true, because B extends A. So you'd first have to check for a instanceof B.
So, if A a = new B(), then instanceof B and instanceof A are both true?
OK. I got it. if A a = new A(), then only instanceof A is true. this is why I have to check B first. Thank you .
@AllenYang You should select this answer or another if you felt that it answered your question best. This gives closure to this question and for anyone else visiting this question in the future.
0

Renuka Fernando is correct, but you have to be careful here. Because of how objects are set up in memory, if your object is declared as a super class, but then initialized to one of its children, like so:

A a = new B();

Then the following code will always says "I'm an A!":

if(a instanceof A)
    System.out.println("I'm an A!");
else
    System.out.println("I'm a B!");

That is because a is an A and a B at the same time, so if you were trying to see if a was a B, then you would have to put in further checks.

Comments

-1

You can use getClass() method of the object instance..

class K {

} 
class P extends K {
}

public class A {
    public static void main(String args[]) {
        K k = new K();
        K p = new P();
        P p1 = new P();

        System.out.println(p.getClass().getName());
        System.out.println(p1.getClass().getName());
        System.out.println(k.getClass().getName());

    }
}

1 Comment

Java has instanceof functionality for a reason. Java reflection should only be used in a few special cases and in other cases is considered bad programming practice, because of the security vulnerabilities that can arise. Obvious your solution above works and does not do anything nasty, but you are strictly avoiding a feature that was made for this exact purpose.

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.