Friday, September 21, 2012

How to determine Type of object at runtime in Java - Runtime Type Identification

Runtime Type identification in Java
Determining Type of object at runtime in Java means finding what kind of object it is. For those who are not familiar with What is a Type in Java,  Type is the name of class e..g for “abc” which is a String object, Type is String. Finding Type of any object at runtime is  also known as Runtime Type Identification in Java. Determining Type becomes increasingly important for method which accept parameter of type java.lang.Object like compareTo method of Comparable class. Since two objects of different type can not be equal to each other if we know how to determine type of Object from object itself than we can, not only avoid ClassCastExcpetion but also optimized the equals method. Java provides three different ways to find type of object at runtime e.g. instanceof keyword, getClass() and isInstance() method of java.lang.Class. Out of all three only getClass() is the one which exactly find Type of object while others also return true if Type of object is super type. As we all know that Java  throws ClassCastException if you try to cast object into wrong type, which makes finding type of Object at runtime even more important for robust Java programs, Though type casting can be minimized by using Generics in Java but you still have some places where you need to cast object. In this Java tutorial we will see examples three ways of determining type of Object from Java program itself. On same note Java programming language does not support RTTI(Runtime type Identification) which was C++ capability to find Object type at runtime but as I said Java has its own way of facilitating this.

Java program to determine Type of object at runtime

Runtime Type  Identification in Java - determine type of Object at runtime
Here is test Java program which demonstrates Runtime type identification in Java or in simple worlds how  to find Type of object. It also examples  that how instanceof, getClass() and isInstance() method works and how can we use them to determine Java object type at runtime. In this Java program we have three classes;  Rule, SystemRule and BusinessRule. Both SystemRule and BusinessRule are sub class of Rule. We will create instance of all three classes and then try to find correct Type for those instances. Reason we are using both Super class and Sub class in this test because both instanceof and isInstance() method of java.lang.Class returns true if object is of sub class and we are testing against super class. Let’s see program now :


/**
 * Java program to determine type of Object at runtime in Java.
 * you can identify type of any object by three ways i..e by using instanceof,
 * getClass() and isInstance() method of java.lang.Class.
 * Java does have capability to find out type of object but its not called
 * as RTTI (Runtime type Identification) in C++.
 *
 * @author Javarevisited
 */

public class RuntimeTypeIdentificationTest {
 
 
    public static void main(String args[]) {
        //creating instance of sub class and storing into type of superclass
        Rule simpleRule = new BusinessRule();
     
        //determining type of object in Java using instanceof keyword
        System.out.println("Checking type of object in Java using instanceof ==>");
        if(simpleRule instanceof Rule){
            System.out.println("System rule is instance of Rule");
        }
        if(simpleRule instanceof SystemRule){
            System.out.println("System rule is instance of SystemRule");
        }
        if(simpleRule instanceof BusinessRule){
            System.out.println("System rule is instance of BusinessRule");
        }
     
        //determining type of object in Java using getClass() method
        System.out.println("Checking type of object in Java using  getClass() ==>");
        if(simpleRule.getClass() == Rule.class){
            System.out.println("System rule is instance of Rule");
        }
        if(simpleRule.getClass() == SystemRule.class){
            System.out.println("System rule is instance of SystemRule");
        }
        if(simpleRule.getClass() == BusinessRule.class){
            System.out.println("System rule is instance of BusinessRule");
        }
     
        //determining type of object in Java using isInstance() method
        //isInstance() is similar to instanceof operator and returns true even
        //if object belongs to sub class.
        System.out.println("Checking type of object in Java using  isInstance() ==>");
        if(Rule.class.isInstance(simpleRule)){
            System.out.println("SystemRule is instance of Rule");
        }
        if(SystemRule.class.isInstance(simpleRule)){
            System.out.println("SystemRule is instance of SystemRule");
        }
        if(BusinessRule.class.isInstance(simpleRule)){
            System.out.println("SystemRule is instance of BusinessRule");
        }
    }
 
}

class Rule{
    public void process(){
        System.out.println("process method of Rule");
    }
}

class SystemRule extends Rule{
 
    @Override
    public void process(){
        System.out.println("process method of SystemRule class");
    }
}

class BusinessRule extends Rule{
 
    @Override
    public void process(){
        System.out.println("process method of Business Rule class");
    }
}

Output:
Checking type of object in Java using instanceof ==>
SystemRule is instance of Rule
SystemRule is instance of BusinessRule

Checking type of object in Java using  getClass() ==>
SystemRule is instance of BusinessRule

Checking type of object in Java using  isInstance() ==>
SystemRule is instance of Rule
SystemRule is instance of BusinessRule

If you look at the output you will find that both instanceof keyword and isInstance() also consider sub type object as of Super Type. Only getClass() method returns strict type identification result and does not consider sub class object as of Super class. That’s one of the reason programmer prefer to use getClass() over instanceof while overriding equals method in Java.


Important points to remember about Runtime Type Identification in Java
Few points which is worth remembering while determining Type or Class of object from Java program during runtime:

1) Always determine Type while writing methods which accept Object, that not only reduces error but also result in robust program. You can also use Generics feature to write parameterized method which is better than method which accepts raw types.

2) Type identification is also useful before type casting any object into another Type to avoid ClassCastException.

3) Another use of  Runtime Type identification is to implement type specific feature on method which accept general Type e.g. Object or any interface.

That's it on Runtime type identification or determining Type of object at runtime in Java program. we have seen couple of ways to do this like instanceof operator, getClass() and finally isInstance() method of java.lang.Class. If you look at the output closely you might have figured out that except getClass() other two ways of finding Type of object also return true if object is of Super type and that's why getClass() is the preferred way and I always use it while overriding equals() and hashCode() methods. Remember Java does not support Runtime Type Identification (RTTI) as supported in C++ but does provide few API method to find object at runtime.

Other Java fundamental and concept tutorials from Javarevisited

3 comments :

Anonymous said...

What if I do not know which class/ -es to expect so I cannot do the "instanceOf" check? Is there any way?

Javin @ transient vs volatile said...

If you don't know which class to expect than you need to rely on getClass() method to get Class of current object.

Anonymous said...

You have a typo that makes your output all wrong. All of your output statements should read "SIMPLE rule is an instance of...", not "SYSTEM rule is an instance of...". You're testing simpleRule, which you define as a business rule. So calling it "system rule" in output is very confusing.

Post a Comment