instanceof sample program in Java
By: Abinaya Printer Friendly Format
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 at 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 superclass called A can produce two subclasses, called B and C. Thus, casting a B object into type A or casting a C object into type A is legal, but casting a B object into type C (or vice versa) 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 B, a run-time exception will be thrown. Java provides the run-time operator instanceof to answer this question.
The instanceof operator has this general form:
object instanceof 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 instanceof operator evaluates to true. Otherwise, its result is false. Thus, instanceof is the means by which your program can obtain run-time type information about an object.
The following program demonstrates instanceof:
// Demonstrate instanceof operator.
class A {
int i, j;
}
class B {
int i, j;
}
class C extends A {
int k;
}
class D extends A {
int k;
}
class InstanceOf {
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
D d = new D();
if(a instanceof A)
System.out.println("a is instance of A");
if(b instanceof B)
System.out.println("b is instance of B");
if(c instanceof C)
System.out.println("c is instance of C");
if(c instanceof A)
System.out.println("c can be cast to A");
if(a instanceof C)
System.out.println("a can be cast to C");
System.out.println();
// compare types of derived types
A ob;
ob = d; // A reference to d
System.out.println("ob now refers to d");
if(ob instanceof D)
System.out.println("ob is instance of D");
System.out.println();
ob = c; // A reference to c
System.out.println("ob now refers to c");
if(ob instanceof D)
System.out.println("ob can be cast to D");
else
System.out.println("ob cannot be cast to D");
if(ob instanceof A)
System.out.println("ob can be cast to A");
- 230 -
System.out.println();
// all objects can be cast to Object
if(a instanceof Object)
System.out.println("a may be cast to Object");
if(b instanceof Object)
System.out.println("b may be cast to Object");
if(c instanceof Object)
System.out.println("c may be cast to Object");
if(d instanceof Object)
System.out.println("d may be cast to Object");
}
}
The output from this program is shown here:
a is instance of A
b is instance of B
c is instance of C
c can be cast to A
ob now refers to d
ob is instance of D
ob now refers to c
ob cannot be cast to D
ob can be cast to A
a may be cast to Object
b may be cast to Object
c may be cast to Object
d may be cast to Object
The instanceof operator isn't needed by most programs, because, generally, you know the type of object with which you are working. However, it can be very useful when you're writing generalized routines that operate on objects of a complex class hierarchy.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. very useful example. Thanks for this help.
View Tutorial By: Saurabh at 2010-04-23 04:50:13
2. Nice one....
View Tutorial By: vinod at 2010-05-06 23:36:15
3. THANX FOR THE INFO....
View Tutorial By: Abhinaykumar at 2010-07-06 11:34:53
4. nice and simple information...............
View Tutorial By: sengadu at 2010-07-24 07:43:29
5. good example with which i can understand the conce
View Tutorial By: kalpana at 2010-07-26 08:26:58
6. Helpfull!
View Tutorial By: touraj at 2010-08-11 13:40:33
7. Thank u for helping us.......
View Tutorial By: Monojit Saha at 2010-08-16 06:43:31
8. please provide Example instanceof With Collection
View Tutorial By: siddardha at 2010-08-18 00:30:38
9. it wonder full ex to understand instenceof operato
View Tutorial By: bhaskarallu at 2010-08-29 07:05:40
10. NIce explanation ........
View Tutorial By: Rasala at 2010-11-25 00:06:28
11. Excellent example !!!
View Tutorial By: mah at 2010-11-27 04:06:22
12. good one nice
View Tutorial By: megha at 2011-07-16 15:54:29
13. good one nice
View Tutorial By: megha at 2011-07-16 15:54:29
14. good one nice
View Tutorial By: megha at 2011-07-16 15:54:29
15. this has been copied from Herbert Schildt....dude.
View Tutorial By: Adidas at 2011-08-04 19:25:23
16. Thankyou
View Tutorial By: kp at 2011-08-22 16:20:17
17. Good example to understand.
View Tutorial By: chandra sekhar at 2011-08-24 06:38:55
18. very finly explained..
View Tutorial By: vipul at 2011-10-18 14:10:04
19. good.....not bad. .show me more programs.
View Tutorial By: saranya at 2011-12-07 08:48:00
20. good.....not bad. .show me more programs.
View Tutorial By: saranya at 2011-12-07 08:48:48
21. good.....not bad. .show me more programs.
View Tutorial By: saranya at 2011-12-07 08:49:39
22. gr8 help dude thankx a lot:-)
View Tutorial By: Mr. Bond at 2011-12-09 19:21:36
23. Thanks.. grt example
Also... If the type ar
View Tutorial By: Shahid at 2011-12-20 10:26:22
24. simple and clear , thanks a lot!
View Tutorial By: Steiner at 2012-01-29 19:44:57
25. thank you..!!
really very helpful...
View Tutorial By: sneha at 2012-01-30 18:01:16
26. thank u it hlep me a lot gr8.
View Tutorial By: harish at 2012-03-31 14:34:50
27. Java s ssssss
View Tutorial By: zoya at 2012-04-27 12:24:59
28. Java s ssssss
View Tutorial By: zoya at 2012-04-27 12:25:44
29. Nice example. Thank you very much.
View Tutorial By: vinod at 2012-06-19 11:19:53
30. if(a instanceof C)
System.out.println("
View Tutorial By: John at 2012-07-13 17:34:42
31. Great ! done a gud job
View Tutorial By: Manju at 2012-11-02 06:43:58
32. it is a good example thanks
View Tutorial By: tadeleh at 2012-11-13 12:46:08
33. nice
View Tutorial By: saidesh at 2015-01-09 07:14:50
34. JasonNix
View Tutorial By: JasonNix at 2017-04-25 05:54:10
35. >> bienvenidos.
View Tutorial By: Joo Lorenzo at 2017-07-29 09:09:45