|
Technical Interview Questions
Javascript Interview Questions
Oracle Interview Questions
J2EE Interview Questions
C++
Interview Questions
XML
Interview Questions
EJB
Interview Questions
JSP
Interview Questions
.........More
Programming Source Codes
Java Source Codes
Html Source Codes
CSS Source Codes
C Source Codes
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
Java Source Codes
Example of instanceof
Operator
class Base {
}
class Derived extends Base {
}
/**
* Demo for instanceof Operator
* @author jimmi.prajapati
*
*/
public class InstanceofDemo {
static void test(Object x) {
System.out.println("Testing x of type " + x.getClass());
System.out.println("x instanceof Base " + (x instanceof Base));
System.out.println("x instanceof Derived " + (x instanceof Derived));
System.out.println("Base.isInstance(x) " + Base.class.isInstance(x));
System.out.println("Derived.isInstance(x) "
+ Derived.class.isInstance(x));
System.out.println("x.getClass() == Base.class "
+ (x.getClass() == Base.class));
System.out.println("x.getClass() == Derived.class "
+ (x.getClass() == Derived.class));
System.out.println("x.getClass().equals(Base.class)) "
+ (x.getClass().equals(Base.class)));
System.out.println("x.getClass().equals(Derived.class)) "
+ (x.getClass().equals(Derived.class)));
}
public static void main(String[] args) {
test(new Base());
test(new Derived());
}
}
<<<----- Return to
Java Source
Code Questions Page.
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Java Interview
Questions for more Java Interview Questions with answers
Check
Servlet Interview
Questions for more Servlet Interview Questions with answers
Check
Structs Interview
Questions for more Structs Interview Questions with answers
|