0

Let's say I have two classes:

public Class1 {
/* class body*/
}

public Class2 extends Class1 {
 public Class2 (int var1, int var2) {
 super(var11, var2); 
 }
}

Now I need to determine which class object I have. In this case, how can I determine if I have Class1 or Class2 object?

Object var1 = new Class1(1,2);
Object var2 = new Class2(1,2);

var2 instanceof Class2; /*true*/
var2 instanceof Class1 /*true*/

In both cases, instanceof will return true.

user1209216
  • 7,404
  • 12
  • 60
  • 123
  • use getClass(); that returns the type you need – Stultuske Apr 08 '19 at 06:37
  • 2
    someObject.getClass() returns the class of an object. Note however that you should almost never have to do that. Use polymorphism. – JB Nizet Apr 08 '19 at 06:37
  • When you say "class object", do you mean an instance of one of these classes, or a reference to one of these classes? – khelwood Apr 08 '19 at 06:40
  • 2
    Also note that someObject instanceof Class2 will definitely not return true if the object is not an instance of Class2. – JB Nizet Apr 08 '19 at 06:40
  • I mean instance of Class2 or Class1. Below answers are good, but why down vote? – user1209216 Apr 08 '19 at 06:41
  • Sorry I was not specific. Both are Objects, not direct instances. Now I need to determine actual object type – user1209216 Apr 08 '19 at 06:43
  • 1
    @user1209216 what do you mean 'not direct instances'? the downvote is most likely due to little effort to find the solution yourself. Don't know for sure, though, since I didn't downvote – Stultuske Apr 08 '19 at 06:44
  • 2
    This is an [XY Problem](http://xyproblem.info/). Tell us what you *really* need, not a vague issue which you don't seem to be sure about. – Jai Apr 08 '19 at 06:46
  • Question updated – user1209216 Apr 08 '19 at 06:48

2 Answers2

4

You could use equals:

if (this.getClass().equals(Class1.class) {
    // Do something...
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • the Class class doesn't override equals, so it might be better to compare using == – Stultuske Apr 08 '19 at 06:38
  • 1
    @Stultuske May I ask why you think so? The two options are equivalent when `equals` isn't overridden. – Slaw Apr 08 '19 at 06:51
  • @Slaw for one, if equals isn't overridden, it will execute the == comparison. secondly, use .equals(..) and someone not knowing that there is no implementation for equals might believe there is, and might make assumptions of the implementation – Stultuske Apr 08 '19 at 06:52
  • @Stultuske I always assume `equals()` would give me the correct result, if it is implemented by the core API. Anyway, `Class` class is `final`, and `Object.getClass()` is also `final native`, it really won't break unless you messed with reflections. – Jai Apr 08 '19 at 06:58
  • @Jai who's talking about anything breaking? I'm talking about situations where (unexperienced) developers having to maintain code might have wrong expectations. For instance "will the equals compare the entire path, including package, or only the name", ... which is, after all, possible when there is an implementation of equals. Calling a static method through an instance is also possible, and will also yield the right result, yet still it's not recommended to do so – Stultuske Apr 08 '19 at 07:00
1

Test if it isn't an instance of the subclass:

   object instanceof Superclass
&& !(object instanceof Subclass)

Or, use an if/else chain:

if (object instanceof Subclass) {
  // Do something.
} else if (object instanceof Superclass) {
  // Do something else.
}

The choice depends upon what you're actually trying to use this for.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243