Example of ´instanceof´

66,416

Solution 1

boolean t2 = tableModel.getClass().equals(TableModel1.class); //False
boolean t2 = tableModel.getClass().equals(TableModel2.class); //True

Solution 2

You cannot do it with instanceof, but you can do it with getClass:

boolean t1 = tableModel.getClass().equals(TableModel1.class);
boolean t2 = tableModel.getClass().equals(TableModel2.class);

The instanceof operator is intended for checking class hierarchy all the way, down to the java.lang.Object, including the checks for all interfaces. It lets you know if an instance of the object you have can be cast to the type you specified without triggering class cast exception.

getClass, on the other hand, returns the specific class of the given object.

Solution 3

So, how could I differentiate between TableModel1 and TableModel2 using instanceof?

Technically, you can check that tableModel is an instance of TableModel1 and not and instance of TableModel2:

(tableModel instanceof TableModel1) && !(tableModel instanceof TableModel2)

However, I would like to encourage you in the strongest possible terms to avoid any code that branches based on the result of instanceof or getClass(). Such code is very fragile in the face of future changes. If you find yourself doing anything along these lines, it's a strong clue that it may be a good time to revisit your design.

Solution 4

instance of means "is a".

TableModel2 IS A TableModel1.
But TableModel1 IS NOT A TableModel2.
so

package main;

public class TempClass {
    public static void main(String[] args) {
        TableModel1 tableModel1 = new TableModel1();
        TableModel1 tableModel2 = new TableModel2();

        System.out.println(tableModel1 instanceof TableModel1);
        System.out.println(tableModel1 instanceof TableModel2);

        System.out.println(tableModel2 instanceof TableModel1);
        System.out.println(tableModel2 instanceof TableModel2);
    }

    public static class TableModel1 {
    }

    public static class TableModel2 extends TableModel1 {
    }
}

true
false

true
true

Solution 5

You cannot. If you really need to tell one from another, use tableModel.getClass() instead, as in:

boolean t1 = tableModel.getClass() == TableModel1.class;
boolean t2 = tableModel.getClass() == TableModel2.class;

Note though you're deliberately trying to break one of founding principles of OOP, so make sure you cannot avoid this trick before you use it in a real code.

Share:
66,416
Klausos Klausos
Author by

Klausos Klausos

Updated on February 08, 2020

Comments

  • Klausos Klausos
    Klausos Klausos over 4 years
    public class TableModel2 extends TableModel1 { ... }
    
    TableModel2 tableModel = new TableModel2();
    
    boolean t1 = tableModel instanceof TableModel1;
    boolean t2 = tableModel instanceof TableModel2;
    

    In the above example, t1 and t2 are true. So, how could I differentiate between TableModel1 and TableModel2 using instanceof?

  • Klausos Klausos
    Klausos Klausos over 12 years
    public class TableModel2 extends TableModel1 { ... } Therefore "instanceOf" does not work here
  • alf
    alf over 12 years
    What happens if I add TableModel3? TableModel25?
  • kornero
    kornero over 12 years
    tableModel1 and tableModel2 they are both TableModel1. So instance of give true. But tableModel1 IS NOT A TableModel2.
  • Kanagavelu Sugumar
    Kanagavelu Sugumar over 8 years
    Provided NULL check is added for tableModel before doing this, where as instanceof doesn't require it.