G
gk
interface MyInterface {
}
public class MyInstanceTest implements MyInterface {
static String s;
public static void main(String args[]) {
MyInstanceTest t = new MyInstanceTest();
if (t instanceof MyInterface) {
System.out.println("I am true interface");
} else {
System.out.println("I am false interface");
}
if (s instanceof String) {
System.out.println("I am true String");
} else {
System.out.println("I am false String");
}
}
}
output:
I am true interface
I am false String
this is quite confusing.
see the code...
if (t instanceof MyInterface)
t is not polymorphically created even .....its a damn instance of
MyInstanceTest class .
i dont understand how its going to become an instance of MyInterface
and the output "I am true interface"
this would have been if we had the code like this..
MyInterface t = new MyInstanceTest(); // but its not here
And about the String class see the code...
if (s instanceof String)
is this if block is false because of s is not created via new operator
?
Please explain
}
public class MyInstanceTest implements MyInterface {
static String s;
public static void main(String args[]) {
MyInstanceTest t = new MyInstanceTest();
if (t instanceof MyInterface) {
System.out.println("I am true interface");
} else {
System.out.println("I am false interface");
}
if (s instanceof String) {
System.out.println("I am true String");
} else {
System.out.println("I am false String");
}
}
}
output:
I am true interface
I am false String
this is quite confusing.
see the code...
if (t instanceof MyInterface)
t is not polymorphically created even .....its a damn instance of
MyInstanceTest class .
i dont understand how its going to become an instance of MyInterface
and the output "I am true interface"
this would have been if we had the code like this..
MyInterface t = new MyInstanceTest(); // but its not here
And about the String class see the code...
if (s instanceof String)
is this if block is false because of s is not created via new operator
?
Please explain