Using instance of Java Help

Using instance of 

Sometimes, knowing the type of an object during run time is useful. For example, you 'might have one thread of execution that generates various types of objects, and another thread that processes these objects. In this situation, it might be useful for the processing thread to know the type of each object when it receives it. Another situation in which knowledge of an object's type <It run time is important involves casting .In Java an invalid cast causes a run-time error. Many invalid casts can be caught at compile time. However, casts involving class hierarchies can produce invalid casts that can be detected only at run time. For example, a supergrass called A can produce two sub classes, called B and C. Thus, casting a Object into type A or casting a C object into type A is legal, but casting a B object into type C (or vice avers) isn't legal. Because an object of type A can refer to objects of either B or C, how can you know, at run time, what type of object is actually being referred to before attempting the cast to type C? It could be an object of type A, B, or C. If it is an object of type "6, a run-time exception . will be thrown. Java provides the run-time operator instance of to answer this question. There instance of operator has this general form: ' object instance of type' Here, object is an instance of a class, and'type is a class type. If object is of the specified type or can be cast into the specified type, then the instance of operator evaluates to true. Otherwise, its result is false. Thus, instance of is the means by which your program can obtain run-time type information about an object.

The following program demon transliterate of The pittance of operator isn't needed by most programs, be Cause,generally, you know the type' object with which you &reworking. However, it can be very,useful you're writing generalized routines  on objects.of a complex class hierarchy.

Posted on September 17, 2014 in I/O Applets And Other Topics

Share the Story

About the Author

Back to Top
Share This